C#泛型类的用途和说明

class Program
    {
        public class Test<T, S>
        {
           //泛型类的类型参数可用于类成员
            private T name;
            private S age;
            public Test(T Name, S Age)
            {
                this.name = Name;
                this.age = Age;
            }
            public void SetValue()
            {
                Console.WriteLine(name.ToString());
                Console.WriteLine(age.ToString());
                Console.Read();
            }
        }
        static void Main(string[] args)
        {
            Test<string, int> t = new Test<string, int>("xiaojun", 27);
            t.SetValue();

        }
   说明:(1)在泛型类的继承中,父类的类型参数已被实例化,这种情况下子类不一定必须是泛型类;
         (2)父类的类型参数没有被实例化,但来源于子类,也就是说父类和子类都是泛型类,并且二者有相同的类型参数           

posted @ 2016-08-01 22:13  IDOER  阅读(67)  评论(0编辑  收藏  举报