C#泛型

大概有以下几个C#泛型集合类型:

1. List,这是我们应用最多的泛型种类,它对应ArrayList集合。

2. Dictionary,这也是我们平时运用比较多的泛型种类,对应Hashtable集合。

3. Collection对应于CollectionBase

4. ReadOnlyCollection 对应于ReadOnlyCollectionBase,这是一个只读的集合。

5. Queue,Stack和SortedList源码天空,它们分别对应于与它们同名的非泛型类。

看一下这个类:

 
 
  1. PersonCollection  
  2.  
  3. public class PersonCollection : IList  
  4. {  
  5.     private ArrayList _Persons = new ArrayList();  
  6.     public Person this[int index]  
  7.     { get { return (Person) _Persons[index]; } }  
  8.     public int Add(Person item)  
  9.     {  
  10.         _Persons.Add(item);  
  11.         return _Persons.Count - 1;  
  12.     }  
  13.     public void Remove(Person item)  
  14.     { _Persons.Remove(item); }  
  15.  
  16.     object IList.this[int index]  
  17.     {  
  18.         get { return _Persons[index]; }  
  19.         set { _Persons[index] = (Person)value; }  
  20.     }  
  21.     int IList.Add(object item)  
  22.     { return Add((Person)item); }  
  23.     void IList.Remove(object item)  
  24.     { Remove((Person)item); }  
  25.  
  26. }  

这个类是Person类的操作类,可以自由的增加或删除Person类.如果现在要为其他的类写一个功能与这个类一样的操作类,相信只需要将Person类替换一下就可以了.不过在了解泛型之后你就可以这样来用.

 
 
  1. List﹤Person﹥ persons = new List﹤Person﹥();  
  2.  
  3. persons.Add(new Person());  
  4.  
  5. Person person = persons[0];   

比如,如果要将Person类换成Employee类,只需要像这样写.

 
 
  1. List﹤Employee﹥ employees= new List﹤Employee﹥();  
  2.  
  3. employees.Add(new Employee());  
  4.  
  5. Employee employee= employees[0];  

List是C#中已经定义好的泛型类,现在我们可以自己定义它.

 
 
  1. TypeHelper  
  2.  
  3. public class TypeHelper﹤T﹥  
  4.  
  5. {  
  6.   public String GetType(T t){  
  7.  
  8.     return "Type is "+t.GetType().toString();  
  9.   }  
  10. }  

这里的T只是一个类型的占位符,在实际应用的时候,将实际的类型替换掉T就可以.

 
 
  1. TypeHelper﹤Person﹥ typeHelper = new TypeHelper();  
  2.  
  3. typeHelper.GetType(Person);  

注意T只是一个占位符,实际上换上任何符号都可以,千万不要任为只有T可以做占位符.

有的时候我们必须要约束一下实际的类型,比如以下的泛型类.

C#泛型集合类型的使用基本内容就向你介绍到这里,希望对你了解和学习C#泛型集合类型有所帮助。

 
 
  1. CollectionHelper  
  2. public class CollectionHelper﹤T,V﹥  
  3.  
  4. {  
  5.  
  6.       private T Collec = new T();  
  7.  
  8.        public int IndexOf(V v){  
  9.  
  10.            return Collec.IndexOf(v);          
  11.  
  12.       }  
  13. }  

泛型类中的T,V显示不是随便什么类型都可以代替的,首先这个类型T必须具有IndexOf方法,V必须是一个引用类型.所以这个类要修改一下.

 
 
  1. CollectionHelper  
  2. public class CollectionHelper﹤T,V﹥ where T:IList  
  3.  
  4.      where V:class 
  5.  
  6. {  
  7.  
  8.    private T Collec = new List();  
  9.  
  10.    public int IndexOf(V v){  
  11.  
  12.      return Collec.IndexOf(v);          
  13.  
  14.        }  

where 是关键字,T是表示所要进行约束的类型.IList是表示要实现的接口,显示只要实现IList接口,就一定有IndexOf方法,V的约束是必须是一个类.

如果要求类型必须是一个值类型的参数,就需要使用struct.如果还需要将类型重新实例化就需要使用new()来进行限制,说明该类型必须要有一个无参的构造函数.

注意如果一个类型需要有多个约束进行限制,那么new()必须写在最后面.

泛型也可以用到委托里叫泛型委托,不过对于委托本人不是很熟,相信都是大同小异.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值