您的位置:首页 > 编程语言 > C#

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

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

class C<T>
class D<T>
interface IDictionary<T1,T2>
class List<T>:IList<T>,IDictionary<string,T>
delegate bool Predicate<T>(T value);
class C
public class Finder

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  d、泛型方法的重写

class C<T> where T:I1
class C<T> where T:new()
C<A> c = new C<A>();//合法
C<B> c1 = new C<B>();//非法
  d、值/引用类型约束

C<A> c = new C<A>();//合法
C<B> c1 = new C<B>(); //非法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: