What does new() mean in this case? [duplicate]
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.
精彩评论