C# 什么是;其中T:class,new();什么意思?
你能给我解释下一行代码中T:class,new()的意思吗C# 什么是;其中T:class,new();什么意思?,c#,generics,type-constraints,C#,Generics,Type Constraints,你能给我解释下一行代码中T:class,new()的意思吗 void Add<T>(T item) where T : class, new(); void Add(T项),其中T:class,new(); 这是对通用参数T的约束。它必须是类(引用类型),并且必须有一个公共参数减去默认构造函数 这意味着T不能是int、float、double、DateTime或任何其他struct(值类型) 它可以是字符串,也可以是任何其他自定义引用类型,只要它有一个默认的或无参数的构造函数。这意
void Add<T>(T item) where T : class, new();
void Add(T项),其中T:class,new();
这是对通用参数T
的约束。它必须是类
(引用类型),并且必须有一个公共参数减去默认构造函数
这意味着T
不能是int
、float
、double
、DateTime
或任何其他struct
(值类型)
它可以是
字符串
,也可以是任何其他自定义引用类型,只要它有一个默认的或无参数的构造函数。这意味着类型T
必须是一个类,并且有一个不带任何参数的构造函数
例如,您必须能够执行以下操作:
T t = new T();
它被称为泛型参数T的“约束”。这意味着T必须是引用类型(类),并且必须具有公共默认构造函数。这是泛型机制的一部分,where关键字向必须实现的类型添加约束,以便用作类型参数。 new()约束让编译器知道,提供的任何类型参数都必须具有可访问的无参数(或默认)构造函数
所以它应该是,
T
必须是一个类,并且具有可访问的无参数构造函数或默认构造函数。这些是泛型类型约束。在您的案例中,有两种:
where T : class
表示类型T
必须是引用类型(不是值类型)
表示类型T
必须具有无参数构造函数。具有此约束将允许您执行类似于T field=new T()的操作代码>在您的代码中,否则您将无法执行
然后,使用逗号将这两者结合起来,得到:
where T : class, new()
其中T:struct
类型参数必须是值类型。可以指定除Nullable之外的任何值类型。有关更多信息,请参阅使用可为空的类型(C#编程指南)
其中T:class
类型参数必须是引用类型,包括任何类、接口、委托或数组类型。(见下文注释。)
其中T:new()
类型参数必须具有公共无参数构造函数。当与其他约束一起使用时,必须最后指定new()约束
其中T:[基类名称]
类型参数必须是或派生自指定的基类
其中T:[接口名称]
类型参数必须是或实现指定的接口。可以指定多个接口约束。约束接口也可以是泛型的
其中T:U
为T提供的类型参数必须是或派生自为U提供的参数。这称为裸类型约束。new():指定new()约束意味着类型T必须使用无参数构造函数,以便可以从中实例化对象-请参阅
类:表示T必须是引用类型,因此不能是int、float、double、DateTime或其他结构(值类型)
public void MakeCars()
{
//这不会编译,因为researchEngine没有公共构造函数,因此无法实例化。
CarFactory researchLine=新的CarFactory();
var researchEngine=researchLine.MakeEngine();
//可以使用默认公共构造函数实例化类的新对象
CarFactory productionLine=新的CarFactory();
var productionEngine=productionLine.MakeEngine();
}
公共类ProductionEngine{}
公共类研究引擎
{
专用研究引擎(){}
}
公共级汽车制造厂,其中TEngine:class,new()
{
公共引擎()
{
返回新的TEngine();
}
}
在“Where”之后是对您声明的泛型类型T的约束,因此:
- class意味着T应该是一个类,而不是一个值类型或结构
- new()表示T类应该定义一个无公共参数的默认构造函数
类
和新
为2。
它们分别确保:
class
类型参数必须是引用类型;这也适用于任何类、接口、委托或数组类型
new
类型参数必须具有公共无参数构造函数。与其他约束一起使用时,必须最后指定new()约束
它们的组合意味着类型T
必须是a(不能是a),并且必须具有无参数构造函数
例如:
struct MyStruct { } // structs are value types
class MyClass1 { } // no constructors defined, so the class implicitly has a parameterless one
class MyClass2 // parameterless constructor explicitly defined
{
public MyClass2() { }
}
class MyClass3 // only non-parameterless constructor defined
{
public MyClass3(object parameter) { }
}
class MyClass4 // both parameterless & non-parameterless constructors defined
{
public MyClass4() { }
public MyClass4(object parameter) { }
}
interface INewable<T>
where T : new()
{
}
interface INewableReference<T>
where T : class, new()
{
}
class Checks
{
INewable<int> cn1; // ALLOWED: has parameterless ctor
INewable<string> n2; // NOT ALLOWED: no parameterless ctor
INewable<MyStruct> n3; // ALLOWED: has parameterless ctor
INewable<MyClass1> n4; // ALLOWED: has parameterless ctor
INewable<MyClass2> n5; // ALLOWED: has parameterless ctor
INewable<MyClass3> n6; // NOT ALLOWED: no parameterless ctor
INewable<MyClass4> n7; // ALLOWED: has parameterless ctor
INewableReference<int> nr1; // NOT ALLOWED: not a reference type
INewableReference<string> nr2; // NOT ALLOWED: no parameterless ctor
INewableReference<MyStruct> nr3; // NOT ALLOWED: not a reference type
INewableReference<MyClass1> nr4; // ALLOWED: has parameterless ctor
INewableReference<MyClass2> nr5; // ALLOWED: has parameterless ctor
INewableReference<MyClass3> nr6; // NOT ALLOWED: no parameterless ctor
INewableReference<MyClass4> nr7; // ALLOWED: has parameterless ctor
}
struct MyStruct{}//结构是值类型
类MyClass1{}//未定义构造函数,因此该类隐式具有无参数的构造函数
类MyClass2//显式定义的无参数构造函数
{
公共MyClass2(){}
}
类MyClass3//仅定义了非无参数构造函数
{
公共MyClass3(对象参数){}
}
类MyClass4//定义了无参数和非无参数构造函数
{
公共MyClass4(){}
公共MyClass4(对象参数){}
}
接口不可逆
其中T:new()
{
}
接口不可逆引用
其中T:class,new()
{
}
等级检查
{
INewable cn1;//允许:具有无参数的ctor
INewable n2;//不允许:无无参数
INewable n3;//允许:具有无参数的ctor
INewable n4;//允许:具有无参数的ctor
INewable n5;//允许:具有无参数的ctor
INewable n6;//不允许:无无参数
INewable n7;//允许:具有无参数的ctor
INewableReference nr1;//不允许:不是引用类型
INewableReference nr2;//不允许:无无参数
INewableReference nr3;//不允许
public void MakeCars()
{
//This won't compile as researchEngine doesn't have a public constructor and so can't be instantiated.
CarFactory<ResearchEngine> researchLine = new CarFactory<ResearchEngine>();
var researchEngine = researchLine.MakeEngine();
//Can instantiate new object of class with default public constructor
CarFactory<ProductionEngine> productionLine = new CarFactory<ProductionEngine>();
var productionEngine = productionLine.MakeEngine();
}
public class ProductionEngine { }
public class ResearchEngine
{
private ResearchEngine() { }
}
public class CarFactory<TEngine> where TEngine : class, new()
{
public TEngine MakeEngine()
{
return new TEngine();
}
}
struct MyStruct { } // structs are value types
class MyClass1 { } // no constructors defined, so the class implicitly has a parameterless one
class MyClass2 // parameterless constructor explicitly defined
{
public MyClass2() { }
}
class MyClass3 // only non-parameterless constructor defined
{
public MyClass3(object parameter) { }
}
class MyClass4 // both parameterless & non-parameterless constructors defined
{
public MyClass4() { }
public MyClass4(object parameter) { }
}
interface INewable<T>
where T : new()
{
}
interface INewableReference<T>
where T : class, new()
{
}
class Checks
{
INewable<int> cn1; // ALLOWED: has parameterless ctor
INewable<string> n2; // NOT ALLOWED: no parameterless ctor
INewable<MyStruct> n3; // ALLOWED: has parameterless ctor
INewable<MyClass1> n4; // ALLOWED: has parameterless ctor
INewable<MyClass2> n5; // ALLOWED: has parameterless ctor
INewable<MyClass3> n6; // NOT ALLOWED: no parameterless ctor
INewable<MyClass4> n7; // ALLOWED: has parameterless ctor
INewableReference<int> nr1; // NOT ALLOWED: not a reference type
INewableReference<string> nr2; // NOT ALLOWED: no parameterless ctor
INewableReference<MyStruct> nr3; // NOT ALLOWED: not a reference type
INewableReference<MyClass1> nr4; // ALLOWED: has parameterless ctor
INewableReference<MyClass2> nr5; // ALLOWED: has parameterless ctor
INewableReference<MyClass3> nr6; // NOT ALLOWED: no parameterless ctor
INewableReference<MyClass4> nr7; // ALLOWED: has parameterless ctor
}