Simple question:
If you have a string x
, to initialize it you simple do one of the following:
简单问题:如果您有一个字符串x,要初始化它,您只需执行以下操作之一:
string x = String.Empty;
or
string x = null;
What about Generic parameter T?
通用参数T怎么样?
I've tried doing:
我试过了:
void someMethod<T>(T y)
{
T x = new T();
...
}
Generate error :
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
生成错误:无法创建变量类型“T”的实例,因为它没有new()约束
5 个解决方案
#1
28
You have two options:
你有两个选择:
You can constrain T: you do this by adding: where T : new()
to your method. Now you can only use the someMethod
with a type that has a parameterless, default constructor (see Constraints on Type Parameters).
你可以约束T:你这样做是通过在你的方法中添加:其中T:new()。现在,您只能将someMethod与具有无参数的默认构造函数的类型一起使用(请参阅类型参数上的约束)。
Or you use default(T)
. For a reference type, this will give null
. But for example, for an integer value this will give 0
(see default Keyword in Generic Code).
或者您使用默认值(T)。对于引用类型,这将给出null。但是,例如,对于整数值,这将给出0(参见通用代码中的默认关键字)。
Here is a basic console application that demonstrates the difference:
这是一个基本的控制台应用程序,它演示了差异:
using System;
namespace *
{
class Program
{
public static T SomeNewMethod<T>()
where T : new()
{
return new T();
}
public static T SomeDefaultMethod<T>()
where T : new()
{
return default(T);
}
struct MyStruct { }
class MyClass { }
static void Main(string[] args)
{
RunWithNew();
RunWithDefault();
}
private static void RunWithDefault()
{
MyStruct s = SomeDefaultMethod<MyStruct>();
MyClass c = SomeDefaultMethod<MyClass>();
int i = SomeDefaultMethod<int>();
bool b = SomeDefaultMethod<bool>();
Console.WriteLine("Default");
Output(s, c, i, b);
}
private static void RunWithNew()
{
MyStruct s = SomeNewMethod<MyStruct>();
MyClass c = SomeNewMethod<MyClass>();
int i = SomeNewMethod<int>();
bool b = SomeNewMethod<bool>();
Console.WriteLine("New");
Output(s, c, i, b);
}
private static void Output(MyStruct s, MyClass c, int i, bool b)
{
Console.WriteLine("s: " + s);
Console.WriteLine("c: " + c);
Console.WriteLine("i: " + i);
Console.WriteLine("b: " + b);
}
}
}
It produces the following output:
它产生以下输出:
New
s: *.Program+MyStruct
c: *.Program+MyClass
i: 0
b: False
Default
s: *.Program+MyStruct
c:
i: 0
b: False
#2
7
use default
keyword.
使用默认关键字。
T x = default(T);
See: default Keyword in Generic Code (C# Programming Guide)
请参阅:通用代码中的默认关键字(C#编程指南)
Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.
给定参数化类型T的变量t,语句t = null仅在T是引用类型时有效,并且t = 0仅适用于数值类型而不适用于结构。解决方案是使用default关键字,它将为引用类型返回null,为数值类型返回零。对于结构体,它将返回初始化为struct或null的结构的每个成员,具体取决于它们是值还是引用类型。
#3
6
You need to add a new
constraint for the type parameter T
.
您需要为类型参数T添加新约束。
void someMethod<T>(T y) where T : new()
{
T x = new T();
...
}
This will only be valid for types with a default constructor however.
这仅对具有默认构造函数的类型有效。
The where
clause for T
is a generic type constraint. In this case, it requires that any type T
this method is applied to must have a public parameterless constructor.
T的where子句是泛型类型约束。在这种情况下,它要求应用此方法的任何类型必须具有公共无参数构造函数。
#4
3
If you really need an instance of T and not a default null value for reference types, use:
如果您确实需要T的实例而不是引用类型的默认空值,请使用:
Activator.CreateInstance()
#5
0
You may use default
construct to set it to whatever that Type's default is.
您可以使用默认构造将其设置为Type的默认值。
The default keyword allows you to tell the compiler that at compile time the default value of this variable should be used. If the type argument supplied is a numeric value (e.g., int, long, decimal), then the default value is zero. If the type argument supplied is a reference type, then the default value is null. If the type argument supplied is a struct, then the default value of the struct is determined by initializing each member field of the struct to zero for numeric types or null for reference types.
default关键字允许您告诉编译器在编译时应该使用此变量的默认值。如果提供的type参数是数值(例如,int,long,decimal),则默认值为零。如果提供的type参数是引用类型,则默认值为null。如果提供的类型参数是结构,则通过将结构的每个成员字段初始化为数字类型的零或引用类型为null来确定结构的默认值。
Use something like :
使用类似的东西:
T data = default(T);
T data = default(T);
For details, read : Initializing Generic Variables to Their Default Values
有关详细信息,请阅读:将通用变量初始化为其默认值
#1
28
You have two options:
你有两个选择:
You can constrain T: you do this by adding: where T : new()
to your method. Now you can only use the someMethod
with a type that has a parameterless, default constructor (see Constraints on Type Parameters).
你可以约束T:你这样做是通过在你的方法中添加:其中T:new()。现在,您只能将someMethod与具有无参数的默认构造函数的类型一起使用(请参阅类型参数上的约束)。
Or you use default(T)
. For a reference type, this will give null
. But for example, for an integer value this will give 0
(see default Keyword in Generic Code).
或者您使用默认值(T)。对于引用类型,这将给出null。但是,例如,对于整数值,这将给出0(参见通用代码中的默认关键字)。
Here is a basic console application that demonstrates the difference:
这是一个基本的控制台应用程序,它演示了差异:
using System;
namespace *
{
class Program
{
public static T SomeNewMethod<T>()
where T : new()
{
return new T();
}
public static T SomeDefaultMethod<T>()
where T : new()
{
return default(T);
}
struct MyStruct { }
class MyClass { }
static void Main(string[] args)
{
RunWithNew();
RunWithDefault();
}
private static void RunWithDefault()
{
MyStruct s = SomeDefaultMethod<MyStruct>();
MyClass c = SomeDefaultMethod<MyClass>();
int i = SomeDefaultMethod<int>();
bool b = SomeDefaultMethod<bool>();
Console.WriteLine("Default");
Output(s, c, i, b);
}
private static void RunWithNew()
{
MyStruct s = SomeNewMethod<MyStruct>();
MyClass c = SomeNewMethod<MyClass>();
int i = SomeNewMethod<int>();
bool b = SomeNewMethod<bool>();
Console.WriteLine("New");
Output(s, c, i, b);
}
private static void Output(MyStruct s, MyClass c, int i, bool b)
{
Console.WriteLine("s: " + s);
Console.WriteLine("c: " + c);
Console.WriteLine("i: " + i);
Console.WriteLine("b: " + b);
}
}
}
It produces the following output:
它产生以下输出:
New
s: *.Program+MyStruct
c: *.Program+MyClass
i: 0
b: False
Default
s: *.Program+MyStruct
c:
i: 0
b: False
#2
7
use default
keyword.
使用默认关键字。
T x = default(T);
See: default Keyword in Generic Code (C# Programming Guide)
请参阅:通用代码中的默认关键字(C#编程指南)
Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.
给定参数化类型T的变量t,语句t = null仅在T是引用类型时有效,并且t = 0仅适用于数值类型而不适用于结构。解决方案是使用default关键字,它将为引用类型返回null,为数值类型返回零。对于结构体,它将返回初始化为struct或null的结构的每个成员,具体取决于它们是值还是引用类型。
#3
6
You need to add a new
constraint for the type parameter T
.
您需要为类型参数T添加新约束。
void someMethod<T>(T y) where T : new()
{
T x = new T();
...
}
This will only be valid for types with a default constructor however.
这仅对具有默认构造函数的类型有效。
The where
clause for T
is a generic type constraint. In this case, it requires that any type T
this method is applied to must have a public parameterless constructor.
T的where子句是泛型类型约束。在这种情况下,它要求应用此方法的任何类型必须具有公共无参数构造函数。
#4
3
If you really need an instance of T and not a default null value for reference types, use:
如果您确实需要T的实例而不是引用类型的默认空值,请使用:
Activator.CreateInstance()
#5
0
You may use default
construct to set it to whatever that Type's default is.
您可以使用默认构造将其设置为Type的默认值。
The default keyword allows you to tell the compiler that at compile time the default value of this variable should be used. If the type argument supplied is a numeric value (e.g., int, long, decimal), then the default value is zero. If the type argument supplied is a reference type, then the default value is null. If the type argument supplied is a struct, then the default value of the struct is determined by initializing each member field of the struct to zero for numeric types or null for reference types.
default关键字允许您告诉编译器在编译时应该使用此变量的默认值。如果提供的type参数是数值(例如,int,long,decimal),则默认值为零。如果提供的type参数是引用类型,则默认值为null。如果提供的类型参数是结构,则通过将结构的每个成员字段初始化为数字类型的零或引用类型为null来确定结构的默认值。
Use something like :
使用类似的东西:
T data = default(T);
T data = default(T);
For details, read : Initializing Generic Variables to Their Default Values
有关详细信息,请阅读:将通用变量初始化为其默认值