"T:class,new()"的意思是什么?

Raw*_*hi 280 c# generics type-constraints

能否请您解释where T : class, new()以下代码行中的含义?

void Add<T>(T item) where T : class, new();
Run Code Online (Sandbox Code Playgroud)

Ner*_*ury 308

这是对泛型参数的约束T.它必须是class(引用类型),并且必须具有公共参数的默认构造函数.

这意味着T不能是int,float,double,DateTime或任何其他struct(值类型).

它可以是一个string或任何其他自定义引用类型,只要它具有默认或无参数构造函数.

  • 只是为了澄清,如果你没有将class子句作为T ...的一部分,那么使用int,float,double等是安全的. (5认同)
  • 为了澄清Justin的评论,空的构造函数没有语句(例如默认构造函数),而无参数的构造函数可能包含语句(例如初始化列表)。 (3认同)

Jus*_*ner 151

这些是通用类型约束.在你的情况下,有两个:

where T : class
Run Code Online (Sandbox Code Playgroud)

表示类型T必须是引用类型(不是值类型).

where T : new()
Run Code Online (Sandbox Code Playgroud)

表示该类型T必须具有无参数构造函数.拥有此约束将允许您执行类似于T field = new T();代码中的其他操作,否则您将无法执行此操作.

然后使用逗号将两者结合起来:

where T : class, new()
Run Code Online (Sandbox Code Playgroud)


小智 113

其中T:struct

type参数必须是值类型.可以指定除Nullable之外的任何值类型.有关更多信息,请参阅使用可空类型(C#编程指南).

哪里T:上课

type参数必须是引用类型,包括任何类,接口,委托或数组类型.(见下面的注释.)

其中T:new() 类型参数必须具有公共无参数构造函数.与其他约束一起使用时,必须最后指定new()约束.

其中T:[基类名]

type参数必须是或从指定的基类派生.

其中T:[接口名称]

type参数必须是或实现指定的接口.可以指定多个接口约束.约束接口也可以是通用的.

T:U

为T提供的类型参数必须是或者从为U提供的参数派生.这称为裸类型约束.

  • 这很有用,但[链接到源代码](http://msdn.microsoft.com/en-us/library/d5x73970.aspx). (21认同)

Ser*_*gio 18

class&new泛型类型参数的T 2个约束.
他们分别确保:

class

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

new

type参数必须具有公共无参数构造函数.与其他约束一起使用时,必须最后指定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
}
Run Code Online (Sandbox Code Playgroud)


Bre*_*dan 15

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

class:表示 T必须是引用类型,因此它不能是int,float,double,DateTime或其他结构(值类型).

    public void MakeCars()
    {
        //This wont compile as researchEngine doesn't have a public constructor and so cant 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();
        }
    }
Run Code Online (Sandbox Code Playgroud)


Eva*_*ski 6

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

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

T t = new T();
Run Code Online (Sandbox Code Playgroud)


Fre*_*erg 5

其中(C#参考)

new()约束使编译器知道提供的任何类型参数必须具有可访问的无参数(或默认)构造函数

因此,它T必须是一个类,并且具有可访问的无参数-或默认构造函数。


Otm*_*SEN 5

“Where”后面的内容是对您声明的泛型类型 T 的约束,因此:

  • class意味着 T 应该是一个类,而不是值类型或结构。

  • new()指示 T 类应该定义一个公共的无参数默认构造函数。