你能解释一下T: class, new()在下一行代码中是什么意思吗?

void Add<T>(T item) where T : class, new();

当前回答

class和new是泛型类型参数T上的两个约束。 它们分别保证:

type参数必须是引用类型;这也适用于任何类、接口、委托或数组类型。

new

类型参数必须有一个公共的无参数构造函数。当与其他约束一起使用时,new()约束必须在最后指定。

它们的组合意味着类型T必须是引用类型(不能是值类型),并且必须有一个无参数的构造函数。

例子:

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
}

其他回答

new():指定new()约束意味着类型T必须使用无参数构造函数,因此可以从它实例化对象-请参阅默认构造函数。

class:意思是T必须是一个引用类型,所以它不能是int, float, double, DateTime或其他结构(值类型)。

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();
    }
}

这意味着类型T必须是一个类,并且有一个不接受任何参数的构造函数。

例如,你必须能够这样做:

T t = new T();

“Where”后面的是对你声明的泛型类型T的约束,所以:

class意味着T应该是一个类,而不是值类型或结构。 new()表示T类应该定义一个没有公共形参的默认构造函数。

这些是泛型类型约束。在你的例子中有两个:

where T : class

意味着类型T必须是引用类型(而不是值类型)。

where T : new()

意味着类型T必须有一个无参数的构造函数。有了这个约束,你可以这样做T field = new T();在你的代码中,你不能这样做。

然后用逗号将两者结合起来,得到:

where T : class, new()

class和new是泛型类型参数T上的两个约束。 它们分别保证:

type参数必须是引用类型;这也适用于任何类、接口、委托或数组类型。

new

类型参数必须有一个公共的无参数构造函数。当与其他约束一起使用时,new()约束必须在最后指定。

它们的组合意味着类型T必须是引用类型(不能是值类型),并且必须有一个无参数的构造函数。

例子:

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
}