I tried to google this, but all I could find was documents on ordinary class declarations.
我试图谷歌这个,但我能找到的只是普通类声明的文件。
public class DataContextWrapper<T> : IDataContextWrapper where T : DataContext, new()
{
}
I see that the class implements IDataContextWrapper, inherits from DataContext and varies with type T depending on how it is instantiated.
我看到该类实现了IDataContextWrapper,它继承自DataContext,并根据它的实例化方式随T类而变化。
I don't know what "where T
" or the ", new()
" might mean.
我不知道“哪里有T”或“,新()”可能意味着什么。
9 个解决方案
#1
38
It's a generic constraint and restricts what types can be passed into the generic parameter.
它是一个通用约束,并限制可以传递给泛型参数的类型。
In your case it requires that T
is indentical to or derived from DataContext
and has a default(argumentless) constructor(the new()
constraint).
在您的情况下,它要求T与DataContext相同或从DataContext派生,并且具有默认(无参数)构造函数(new()约束)。
You need generic constraints to actually do something non trivial with a generic type.
您需要通用约束来实际使用泛型类型执行一些非常简单的操作。
- The
new()
constraint allows you to create an instance withnew T()
. - new()约束允许您使用新的T()创建实例。
- The
DataContext
constraint allows you to call the methods ofDataContext
on an instance ofT
- DataContext约束允许您在T的实例上调用DataContext的方法
MSDN wrote:
MSDN写道:
where T : <base class name>
The type argument must be or derive from the specified base class.其中T: <基类名> 类型参数必须是或从指定的基类派生。
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.其中T:new()类型参数必须具有公共无参数构造函数。与其他约束一起使用时,必须最后指定new()约束。
#2
13
Only allow types T that are derived from or implement DataContext, and have a public constructor that takes no arguments.
仅允许从派生或实现DataContext的类型T,并且具有不带参数的公共构造函数。
#3
9
It's a generic type constraint and specifies constraint on the generic types (for example, only classes, or must implement a specific interface).
它是泛型类型约束,并指定泛型类型的约束(例如,只有类,或必须实现特定的接口)。
In this case, T
must be a class that is either DataContext
or inherits from it and must have a parameterless public constructor (the new()
constraint).
在这种情况下,T必须是DataContext或从其继承的类,并且必须具有无参数的公共构造函数(new()约束)。
#4
5
It's a generic type restriction. In this case, T must inherit from DataContext and be a type with a constructor that takes no arguments.
这是一种通用类型限制。在这种情况下,T必须从DataContext继承,并且是一个带有不带参数的构造函数的类型。
#5
4
where T: DataContext reads as: T must be a (or derived from a) DataContext the ", new()" reads as: must have an parameterless constructor.
其中T:DataContext读取为:T必须是(或从a派生)DataContext“,new()”读作:必须具有无参数构造函数。
#6
4
The where
keyword is used to constrain your generic type variable, in your case it means that the type T
must be a DataContext
and must contain a public default constructor.
where关键字用于约束泛型类型变量,在您的情况下,它意味着类型T必须是DataContext并且必须包含公共默认构造函数。
#7
2
It is constraints in the types that can be used as generic. This gives you compiler checks plus the ability to do something meaningful with T.
它是可以用作泛型的类型中的约束。这为您提供了编译器检查以及使用T执行有意义的操作的能力。
Ie. new()
tells the compiler that T has to have a parameterless constructor. This means that you can instantiate instances of T by writing new T();
and by knowing T is a DataContext as well, you can both make instances of T but also call methods on it.
IE浏览器。 new()告诉编译器T必须有一个无参数构造函数。这意味着您可以通过编写新的T()来实例化T的实例;并且通过知道T也是一个DataContext,你既可以创建T的实例,也可以调用它的方法。
#8
2
It's a generics constraint. MSDN has more information on that.
这是一种泛型约束。 MSDN有更多相关信息。
See Constraints on Type Parameters (C# Programming Guide)
请参阅类型参数约束(C#编程指南)
#9
2
Where is there to place a constraint upon the type of T. The new says that the type T must be instantiable without any parameters. ie T thing = new T();
在哪里对T的类型设置约束。新的说明类型T必须是可实例化的,没有任何参数。即T thing = new T();
See more here
在这里查看更多
#1
38
It's a generic constraint and restricts what types can be passed into the generic parameter.
它是一个通用约束,并限制可以传递给泛型参数的类型。
In your case it requires that T
is indentical to or derived from DataContext
and has a default(argumentless) constructor(the new()
constraint).
在您的情况下,它要求T与DataContext相同或从DataContext派生,并且具有默认(无参数)构造函数(new()约束)。
You need generic constraints to actually do something non trivial with a generic type.
您需要通用约束来实际使用泛型类型执行一些非常简单的操作。
- The
new()
constraint allows you to create an instance withnew T()
. - new()约束允许您使用新的T()创建实例。
- The
DataContext
constraint allows you to call the methods ofDataContext
on an instance ofT
- DataContext约束允许您在T的实例上调用DataContext的方法
MSDN wrote:
MSDN写道:
where T : <base class name>
The type argument must be or derive from the specified base class.其中T: <基类名> 类型参数必须是或从指定的基类派生。
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.其中T:new()类型参数必须具有公共无参数构造函数。与其他约束一起使用时,必须最后指定new()约束。
#2
13
Only allow types T that are derived from or implement DataContext, and have a public constructor that takes no arguments.
仅允许从派生或实现DataContext的类型T,并且具有不带参数的公共构造函数。
#3
9
It's a generic type constraint and specifies constraint on the generic types (for example, only classes, or must implement a specific interface).
它是泛型类型约束,并指定泛型类型的约束(例如,只有类,或必须实现特定的接口)。
In this case, T
must be a class that is either DataContext
or inherits from it and must have a parameterless public constructor (the new()
constraint).
在这种情况下,T必须是DataContext或从其继承的类,并且必须具有无参数的公共构造函数(new()约束)。
#4
5
It's a generic type restriction. In this case, T must inherit from DataContext and be a type with a constructor that takes no arguments.
这是一种通用类型限制。在这种情况下,T必须从DataContext继承,并且是一个带有不带参数的构造函数的类型。
#5
4
where T: DataContext reads as: T must be a (or derived from a) DataContext the ", new()" reads as: must have an parameterless constructor.
其中T:DataContext读取为:T必须是(或从a派生)DataContext“,new()”读作:必须具有无参数构造函数。
#6
4
The where
keyword is used to constrain your generic type variable, in your case it means that the type T
must be a DataContext
and must contain a public default constructor.
where关键字用于约束泛型类型变量,在您的情况下,它意味着类型T必须是DataContext并且必须包含公共默认构造函数。
#7
2
It is constraints in the types that can be used as generic. This gives you compiler checks plus the ability to do something meaningful with T.
它是可以用作泛型的类型中的约束。这为您提供了编译器检查以及使用T执行有意义的操作的能力。
Ie. new()
tells the compiler that T has to have a parameterless constructor. This means that you can instantiate instances of T by writing new T();
and by knowing T is a DataContext as well, you can both make instances of T but also call methods on it.
IE浏览器。 new()告诉编译器T必须有一个无参数构造函数。这意味着您可以通过编写新的T()来实例化T的实例;并且通过知道T也是一个DataContext,你既可以创建T的实例,也可以调用它的方法。
#8
2
It's a generics constraint. MSDN has more information on that.
这是一种泛型约束。 MSDN有更多相关信息。
See Constraints on Type Parameters (C# Programming Guide)
请参阅类型参数约束(C#编程指南)
#9
2
Where is there to place a constraint upon the type of T. The new says that the type T must be instantiable without any parameters. ie T thing = new T();
在哪里对T的类型设置约束。新的说明类型T必须是可实例化的,没有任何参数。即T thing = new T();
See more here
在这里查看更多