C#2.0 泛型初探 (特性一览)

1、C#泛型由CLR在运行时支持,编译为IL时使用占位符和特列的IL指令,JIT时再真正的泛型实例化,区别于C++的编译时模板机制和Java的搽试法。
    a、可在各CLR语言之间无缝结合;
    b、不会像C++产生代码膨胀问题;
    c、也不会像Java的“假”泛型一样在性能上无所提升;
    d、对于值类型,每种值类型实例化一份,对于引用类型,只实例化为同一份代码。
2、泛型类的要求
   

class A <T1,T2>{}//合法
class B:C <string,int>{} //合法
class D<U,V>:C<U,V>{} //合法
class E<U,V>:C<string,int>{} //合法
class G:C<U,V>{} //非法
/*
总之就是父类的类型参数要么从子类得到确定,如第三个,要么已经确定,如第四个,第五个的父类不能确定类型参数
*/

3、泛型类可以成为其它类的成员

class C<T>
{
}

class D<T>
{
  
public C<T> c;
  
  
public void DoThings()
  
{
    c.ToString();
//没有用约束时,只能用object型的方法。
  }

}

4、泛型接口的要求同2点

interface IList<T>{T[] GetElements();}
interface IDictionary<T1,T2>
{
  
void Add(T1 key, T2 value);
}

class List<T>:IList<T>,IDictionary<string,T>
{
  
public T[] GetElements(){}
  
public void Add(string key, T value){}
}

5、泛型委托

delegate bool Predicate<T>(T value);
class C
{
  
static bool F(int i ){}
  
static bool G(string s){}
  
static void Main()
  
{
    Predicate
<string> p1 = G;//C#2.0新语法
    Predicate<int> p2 = new Predicate<int>(F);//旧式语法写法
}
  d、泛型方法的重写

abstract class Base{
  
public abstract T Function<T,T1>(T t, T1 t1)where T1:T;
  
public abstract T Function2<T> (T t) where T:IComparable;
}


class Derived:Base{
  
public override string Function<string,int>(string s, int i){}
  
public override string Function2<string> (string s) where string:IComparable{}//非法,重载无需声明约束
}

7、泛型约束--C#泛型的类型安全特点--要求显式的约束
  a、基类约束

class Base{public void F1(){}}
class Derived<T> where T:Base{
 
public void F2(T t)
 
{
  t.F1();
//通过约束,能直接调用Base的方法
 }

}


  b、接口约束

interface I1{void F1();}
class C<T> where T:I1
{
  
//可以调用T继承的I1的F1方法
}


  c、构造器约束

class A{public A(){}}
class B{public B(int i){}}
class C<T> where T:new()
{
 
//可以直接用T t = new T();实例化一个对象
}

C
<A> c = new C<A>();//合法
C<B> c1 = new C<B>();//非法


  d、值/引用类型约束

public struct A{}
public class B{}
public class C<T> where T:struct{}
C
<A> c = new C<A>();//合法
C<B> c1 = new C<B>(); //非法

6、泛型方法--在方法声明上包含类型参数
  a、C#不支持泛型索引、属性、事件、构造、析构,但这些成员可以使用类的泛型参数;
  b、泛型方法可以用在泛和非泛类中

public class Finder
{
  
public static Find<T>(T[] items, T item)
  
{
    
for(int i = 0 ; i < items.length ; i++)
    
{
      
if(items[i].Equal(item))return i;
    }

    
return -1;
  }

}



int i = Finder.Find<int>(new int[]{1,2,3,4,5},2);

  c、泛型方法重载要求

void F1<T>(T[] a,int i);
void F1<U>(U[] a,int i); // Can't

void F2<T>(int i );
void F2(int i ); //yes

void F3 <T>(T t) where T:A
void F3 <T>(T t) where T:B //Can't

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值