where T : NET支持的类型参数约束 泛型约束

public static T GetCurrentObjectContext<T>() where T : ODAFDBEntities, new()

查找后发现这是类型参数约束,.NET支持的类型参数约束有以下五种:

  1. where T :struct //约束T必须为值类型

  2. where K : class //约束K必须为引用类型

  3. where V : IComparable //约束V必须是实现了IComparable接口

  4. where W : K //要求W必须是K类型,或者K类型的子类

  5. where X :class ,new () // X必须是引用类型,并且要有一个无参的构造函数(对于一个类型有多有约束,中间用逗号隔开)

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

猜你喜欢

转载自blog.csdn.net/qq_24600981/article/details/81078395