新手菜鸟学习C#的笔记总结 之泛型

本人是初学菜鸟,有错误的地方欢迎大家指正。


什么是泛型泛型是一种特殊的类型,它在声明并实例化类或方法的时候指定类型。个人理解,比如泛型T,就像一种占位符,告诉编译器,我要在这个位置使用一种类型,具体的哪种类型要在具体使用的时候给出。比如class A<T>中,T为泛型,在使用时一般是这样的:A<int> a=new A<int>();其中,int替代了T,告诉编译器,使用的是int类型, A<int>看成一种独立的类型,


为什么要使用泛型:为了使代码更有通用性

比如要写一个交换的方法。交换的内容根据应用场合不同而变化,比如有的场合需要交换int型,有的需要交换double型。当然,我们可以采用这样的办法,复制两份。

public void Chang(ref int a, ref int b)
        {
            int c;
            c = a;
            a = b;
            b = c;
        }
        public void Chang(ref double a,ref double b)
        {
            double c;
            c = a;
            a = b;
            b = c;
        }

或许我们可以用一个方法,像这样:

 public void Chang(ref object a,ref object b)
        {
            object c;
            c = a;
            a = b;
            b = c;
        }

上面的方法运用object,在传递值类型时需要"封箱"和"拆箱",这样效率就低了。若传递的参数是字符串,而且就会碰到问题

     static void Main(string[] args)
        { 
            String a= "a";
            String b="b";
            Test one =new Test() ;
            one.Chang(ref a, ref b);//one.Chang(ref (Object)a, ref (Object)b);  //无论如何,也无法成功。也许与字符串不可修改有关
            Console.WriteLine("{0}\n{1}",a,b);
         }

所以,我们可以采用泛型,同时效率也提高了。在使用时再决定使用什么变量。

 class Test<T>//<U> where U:struct,IComparable
    {
        public void Chang(ref T a,ref T b)
        {
            T c;
            c = a;
            a = b;
            b = c;
        }
     }
  class Program
    {
        static void Main(string[] args)
        {
            string a = "a";
            string b = "b";
            Test<string> t1 = new Test<string>();
            t1.Chang(ref a,ref b);
            Console.WriteLine("{0}\n{1}",a,b);         
         }
    }
输出结果:

b

a


泛型的使用:运用广泛,可以运用在类、接口、委托、方法上。

1,类的泛型

 class Test<T>//该泛型T在整个类中指代同一种类型,在实例化时Test<xxx>整体作为一个独立的类型,若<>中的xxx不同,类型也不同。
    {
        public void Chang(ref T a,ref T b)
        {
            T c;
            c = a;
            a = b;
            b = c;
        }
     }

2,接口的泛型

public interface TvInterface<T>//与类类似,在使用时,必须给T一个实际的类型,不同的T,表示不同的接口。
    {
        void Display(T content);
    }

3,委托的泛型

public delegate void MyDelegate<T>(T item);//T的类型决定可以连接怎么样的方法

4,方法的泛型

 class Test
    {
        public void Chang<T>(ref T a,ref T b)//泛型方法,T在方法体内通用。
        {
            T c;
            c = a;
            a = b;
            b = c;
        }
     }
  class Program
    {
        static void Main(string[] args)
        {
            string a = "a";
            string b = "b";
            Test t1 = new Test();
            t1.Chang<string>(ref a,ref b);
            Console.WriteLine("{0}\n{1}",a,b);   
         }
     }


泛型参数类型的约束

例如:class Test<T> where T: 约束

1,类型约束

where T: struct 类型参数必须为值类型。
where T : class 类型参数必须为类型。
where T : new() 类型参数必须有一个公有、无参的构造函数。当于其它约束联合使用时,new()约束必须放在最后。
where T : <base class name> 类型参数必须是指定的基类型或是派生自指定的基类型。
where T : <interface name> 类型参数必须是指定的接口或是指定接口的实现。可以指定多个接口约束。接口约束也可以是泛型的。

2.无限制类型参数

MyClass<T>{...}

3,无类型约束

class List<T>{ void Add<U>(List<U> items) whereU:T {…}}


(详细情况可参考:http://birdshover.cnblogs.com/articles/392127.html)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值