泛型是具有占位符(类型参数)的类、结构、接口和方法,这些占位符是类、结构、接口和方法所存储或使用的一个或多个类型的占位符。类型参数使得设计类和方法时,不必确定一个或多个具体参数,具体参数可延迟到客户代码中声明、实现。使用泛型类型可以最大限度地重用代码、保护类型的安全以及提高性能。就像我们写一个类MyList<T>,客户代码可以这样调用:MyList<int>, MyList<string>或 MyList<MyClass>。方便我们设计更加通用的类型,也避免了容器操作中的装箱和拆箱操作。
PS:为什么要避免装箱和拆箱?
我们知道值类型存放在堆栈上,引用类型存放在堆 上。
(1)装箱:CLR需要做额外的工作把堆栈上的值类型移动到堆上,这个操作就被称为装箱.
(2)拆箱:装箱操作的反操作,把堆中的对象复制到堆栈中,并且返回其值。
装箱和拆箱都意味着堆和堆栈空间的一系列操作,毫无疑问,这些操作的性能代价是很大的,尤其对于堆上空间的操作,速度相对于堆栈的操作慢得多,并且可能引发垃圾回收,这些都将大规模地影响系统的性能。
1.泛型的约束
在指定一个类型参数时,可以指定类型参数必须满足的约束条件,约束是使用 where 上下文关键字指定的。
下表列出了五种类型的约束:
| 约束 | 说明 |
|---|---|
|
T:struct |
类型参数必须是值类型。可以指定除 Nullable 以外的任何值类型。 |
|
T:class |
类型参数必须是引用类型,包括任何类、接口、委托或数组类型。 |
|
T:new() |
类型参数必须具有无参数的公共构造函数。当与其他约束一起使用时,new() 约束必须最后指定。 |
|
T:<基类名> |
类型参数必须是指定的基类或派生自指定的基类。 |
|
T:<接口名称> |
类型参数必须是指定的接口或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的。 |
|
T:U |
为 T 提供的类型参数必须是为 U 提供的参数或派生自为 U 提供的参数。这称为裸类型约束. |
1.1.派生约束
基本形式 where T:base-class-name
1.1.1.常见的
public class MyClass<T> where T :IComparable { }
1.1.2.约束放在类的实际派生之后
public class B { } public class MyClass<T> : B where T : IComparable { }
1.1.3.可以继承一个基类和多个接口,且基类在接口前面
基本形式 where T:interface-name
public class B { } public class MyClass<T> where T : B, IComparable, ICloneable { }
1.2.构造函数约束
基本形式: where T : new()
1.2.1.常见的
public class MyClass<T> where T : new() { }
1.2.2.可以将构造函数约束和派生约束组合起来,前提是构造函数约束出现在约束列表的最后
public class MyClass<T> where T : IComparable, new() { }
1.3.值约束
基本形式:where T : struct
1.3.1.常见的
public class MyClass<T> where T : struct { }
1.3.2.与接口约束同时使用,在最前面(不能与基类约束,构造函数约束一起使用)
public class MyClass<T> where T : struct, IComparable { }
1.4.引用约束
基本形式:where T : class
1.4.1.常见的
public class MyClass<T> where T : class { }
1.5.多个泛型参数
基本形式:where T : class where u:class
public class MyClass<T, U> where T : IComparable where U : class { }
1.6.继承和泛型
public class B<T>{ }
1.6.1. 在从泛型基类派生时,可以提供类型实参,而不是基类泛型参数
public class SubClass : B<int>{ }
1.6.2.如果子类是泛型,而非具体的类型实参,则可以使用子类泛型参数作为泛型基类的指定类型
public class SubClass<R> : B<R>{ }
1.6.3.在子类重复基类的约束(在使用子类泛型参数时,必须在子类级别重复在基类级别规定的任何约束)
public class B<T> where T : ISomeInterface { } public class SubClass2<T> : B<T> where T : ISomeInterface { }
1.6.4.构造函数约束
public class B<T> where T : new() { public T SomeMethod() { return new T(); } } public class SubClass3<T> : B<T> where T : new(){ }
2.泛型继承
1、泛型类继承中,父类的类型参数已被实例化,这种情况下子类不一定必须是泛型类;
2、父类的类型参数没有被实例化,但来源于子类,也就是说父类和子类都是泛型类,并且二者有相同的类型参数;
/如果这样写的话,显然会报找不到类型T,S的错误 public class TestChild : Test< T, S> { } //正确的写法应该是 public class TestChild : Test< string, int>{ } public class TestChild< T, S> : Test< T, S> { } public class TestChild< T, S> : Test< String, int> { }