where T : class含义

.NET支持的类型参数约束有如下五种:函数

where T : struct                               | T必须是一个结构类型
where T : class                                | T必须是一个Class类型
where T : new()                               | T必需要有一个无参构造函数
where T : NameOfBaseClass          | T必须继承名为NameOfBaseClass的类
where T : NameOfInterface             | T必须实现名为NameOfInterface的接口继承

 

泛型的Where接口

  泛型的Where可以对类型参数做出限定。有如下几种方式。class

  ·where T : struct 限制类型参数T必须继承自System.ValueType。
 
  ·where T : class 限制类型参数T必须是引用类型,也就是不能继承自System.ValueType。泛型

  ·where T : new() 限制类型参数T必须有一个缺省的构造函数构造函数

  ·where T : NameOfClass 限制类型参数T必须继承自某个类或实现某个接口。引用

  以上这些限定能够组合使用,好比: public class Point where T : class, IComparable, new()new

相关文章
相关标签/搜索