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>(); //非法
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>(); //非法
相关文章推荐
- C#2.0 泛型初探 (特性一览)
- C#2.0新特性之泛型编程
- c#2.0的新特性--泛型
- 第1节 C#2.0新特性之一:泛型
- c#2.0新特性:(一)泛型(Generics)
- C#2.0新特性探究之模拟泛型和内置算法
- C# 2.0 新特性(泛型、可空类型)应用一例
- C#2.0新特性探究之模拟泛型和内置算法
- asp.net 2.0教程 C# 2.0新特性 泛型
- C#2.0新特性之泛型与迭代器
- 学习C#2.0新特性:泛型!
- c#2.0新特性:(一)泛型(Generics)
- C#2.0新特性-泛型
- asp.net 2.0教程 C# 2.0新特性 泛型
- C#2.0新特性探究之模拟泛型和内置算法
- C#2.0 Specification(泛型一)
- C# 2.0新特性与C# 3.5新特性[转]
- C# 2.0 新特性之迭代器, Yield Return
- C#新特性——泛型
- C# 2.0与泛型