C# 泛型集合SortedList

iamzxf發表於2015-05-08


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace sortedListT
{
    class Student : IComparable
    {
        public string Name{get;set;}
        public int Age { get; set; }

        public Student() { }
        public Student(string name, int age) 
        {
            this.Age = age;
            this.Name = name;
        }
        public void SayHi()
        {
            Console.WriteLine("my name is {0}, I am {1} years old.",Name,Age);
        }

        public int CompareTo(object other)
        {
            return this.Name.CompareTo(((Student)other).Name);
        }
    }

    class Score
    { 
        public int Cprogram{get;set;}
        public int Java { get; set; }

        public Score(int score1, int score2)
        {
            Cprogram = score1;
            Java = score2;
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            SortedList<Student, Score> students = new SortedList<Student, Score>();
            students.Add(new Student("zhangsan", 23), new Score(78, 90));
            students.Add(new Student("lisi", 24), new Score(88, 99));
            students.Add(new Student("wangwu", 25), new Score(90, 80));

            foreach (Student stu in students.Keys)
                stu.SayHi();

            for (int i = 0; i < students.Count; i++)
            {
                Console.WriteLine("{0,10}\t{1}\t{2}\t{3}", students.Keys[i].Name, students.Keys[i].Age, students.Values[i].Cprogram, students.Values[i].Java);
            }

            Console.ReadLine();

        }
    }
}



相關文章