开发者

What does new() mean in this case? [duplicate]

开发者 https://www.devze.com 2023-03-13 03:34 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: C#: What does new() mean?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

C#: What does new() mean?

I look at definition of E开发者_如何学Pythonnum.TryParse:

public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct, new();

and wondering what new() means here.


Its a generic type parameter constraint that means that the type for TEnum has to have a public, parameterless constructor.

See here:

http://msdn.microsoft.com/en-us/library/d5x73970.aspx


it's a constraint to the generic parameter. It means that TEnum must have a parameterless public constructor (and allows you to do new TEnum()). Checkout MSDN page for more details and other type of constraints.


It is a generic type constraint that requires that the generic type parameter TEnum must support a default constructor (can be newed up without arguments).


It means that the type TEnum must be able to use

var x = new TEnum();


It basically says that you can only use this on types which have a public parameterless constructor, ie: where you can do:

var something = new TEnum();

This allows you to enforce that you can create the type internally.

For details, see the C# new Constraint.


new() as a generic type restriction means that the type used as the generic parameter must have a constructor with the given parameters; here, it must have a parameterless default constructor.

0

精彩评论

暂无评论...
验证码 换一张
取 消