where T : new() 的含义

where T : new()  的含义

public void Request<T>(List<T> EntityList)  where T : class

{

}

这是参数类型约束,指定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的接口

class Core<T> where T : class, new()

where T:
泛型约束,约束类型T必须具有无参的构造函数
表示T必须是class类型或它的派生类。
new()构造函数约束允许开发人员实例化一个泛型类型的对象。 
一般情况下,无法创建一个泛型类型参数的实例。然而,new()约束改变了这种情况,要求类型参数必须提供一个无参数的构造函数。 
在使用new()约束时,可以通过调用该无参构造函数来创建对象。 
基本形式: where T : new() 

public interface IBaseDAL<T> where T : BaseModel, new()
{
  int Add(T model);
  int Update(T model);
  int Delete(T model);
}
让泛类BaseDAL实现接口:
public class BaseDAL<T> : IBaseDAL<T> where T : BaseModel, new()

原文地址:https://www.cnblogs.com/wfy680/p/14536358.html