.............using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace cs12_1
{
    public enum Comparison
    {
        theFirstComesFirst = 1,
        theSecondComesFirst = 2
    }
    public class Pair<T>
    {
        private T[] thePair = new T[2];
        public delegate Comparison WhichIsFirst(T obj1, T obj2);
        public Pair(T firstObject, T secondObject)
        {
            thePair[0] = firstObject;
            thePair[1] = secondObject;
        }
        public void Sort(WhichIsFirst theDelegatedFunc)
        {
            if (theDelegatedFunc(thePair[0], thePair[1]) == Comparison.theSecondComesFirst)
            {
                T temp = thePair[0];
                thePair[0] = thePair[1];
                thePair[1] = temp;
            }
        }
        public void ReverseSort(WhichIsFirst theDelegatedFunc)
        {
            if (theDelegatedFunc(thePair[0], thePair[1]) == Comparison.theFirstComesFirst)
            {
                T temp = thePair[0];
                thePair[0] = thePair[1];
                thePair[1] = temp;
            }
        }
        public override string ToString()
        {
            return thePair[0].ToString() + ", " + thePair[1].ToString();        }
    }
        public class Dog
        {
            private int weight;
            public Dog(int weight)
            {
                this.weight=weight;
            }
            public static Comparison WhichDogComesFirst(Dog d1,Dog d2)
            {
                return d1.weight>d2.weight?Comparison.theSecondComesFirst:Comparison.theFirstComesFirst;
            }
           public override string  ToString()
           {
              return weight.ToString();
           }
       }
        public class Student
        {
            private string name;
            public Student(string name)
            {
                this.name = name;
            }
            public static Comparison WhichStudentComesFirst(Student s1, Student s2)
            {
                return (String.Compare(s1.name, s2.name) < 0 ?
                    Comparison.theFirstComesFirst :
                    Comparison.theSecondComesFirst);
            }
            public override string ToString()
            {
                return name;
            }
        }
    class Program
    {
        static void Main(string[] args)
        {            Student Jesse = new Student("Jesse");
            Student Stacey = new Student("Stacey");
            Dog Milo = new Dog(65);
            Dog Fred = new Dog(12);
            Pair<Student> studentPair = new Pair<Student>(Jesse, Stacey);
            Pair<Dog> dogPair = new Pair<Dog>(Milo, Fred);
            Console.WriteLine("studentPair\t\t\t:{0}",
                studentPair.ToString());
            Console.WriteLine("dogPair\t\t\t\t:{0}",
                dogPair.ToString());            Pair<Student>.WhichIsFirst theStudentDelegate =
                new Pair<Student>.WhichIsFirst(Student.WhichStudentComesFirst);
            Pair<Dog>.WhichIsFirst theDogDelegate =
                new Pair<Dog>.WhichIsFirst(Dog.WhichDogComesFirst);
            studentPair.Sort(theStudentDelegate);
            Console.WriteLine("After Sort StudentPair\t\t:{0}",
                studentPair.ToString());
            studentPair.ReverseSort(theStudentDelegate);            Console.WriteLine("After ReverseSort studentPair\t:{0}",
                studentPair.ToString());
            dogPair.Sort(theDogDelegate);
            Console.WriteLine("After Sort dogPair\t\t:{0}",dogPair.ToString());
            
                dogPair.ReverseSort(theDogDelegate);
            
            Console.WriteLine("After ReverseSort dogPair\t:{0}",
                dogPair.ToString());
            Console.Read();        }
    }
}
其中的:public class Pair<T>
    {
        private T[] thePair = new T[2];
        public delegate Comparison WhichIsFirst(T obj1, T obj2);
        public Pair(T firstObject, T secondObject)
        {
            thePair[0] = firstObject;
            thePair[1] = secondObject;
        }
 public class Pair<T>,这个<T>是什么?集合吗?这里如何理解?
private T[],又如何理解?class Pair<T>类型的数组?
public delegate Comparison WhichIsFirst(T obj1, T obj2);这里的T又如何理解???

解决方案 »

  1.   

    public class Pair <T>自定义的一个集合类。
      

  2.   

    <T>中的T可以被替换成一个类,可以这样用的类就说明它是一个泛型类.
    LZ去看下泛型相关的知识就知道了.
      

  3.   

    不要被1、2楼误导了,它既不是集合,也不是类...因为没有声明泛型约束,它可以代表任何类型...这个T仅仅是个泛型占位符......这涉及泛型的知识,不是几句话能说清楚的...去看MSDN吧...
      

  4.   

    正在翻MSDN,头昏脑胀开始送分