更新时间:2022-12-08 22:37:21
这是一个泛型约束和限制什么类型都可以传递到泛型参数。
It's a generic constraint and restricts what types can be passed into the generic parameter.
在你的情况下,它要求 T
是张玉峰,或从的DataContext
派生而来,有一个默认的(argumentless)构造函数(即新()
约束)。
In your case it requires that T
is indentical to or derived from DataContext
and has a default(argumentless) constructor(the new()
constraint).
您需要通用的约束,实际上是做一些不平凡了泛型类型。
You need generic constraints to actually do something non trivial with a generic type.
新()
的限制,您可以创建一个实例新T()
。的DataContext
的限制,您可以调用的DataContext
在的实例的方法牛逼
new()
constraint allows you to create an instance with new T()
.DataContext
constraint allows you to call the methods of DataContext
on an instance of T
MSDN写道:
其中T:其中,基础类名称>
类型参数必须是或从指定的基类派生。
where T : <base class name>
The type argument must be or derive from the specified base class.
其中T:新的()
类型参数必须有一个公共的无参数的构造函数。当与其他约束一起使用时,new()约束必须最后指定。
where T : new()
The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.