Possible Duplicate:
C#: What does new() mean?
I look at definition of Enum.TryParse:
public static bool TryParse<TEnum>(string value, out TEnum result) where TEnum : struct, new();
and wondering what new()
means here.
Possible Duplicate:
C#: What does new() mean?
I look at definition of Enum.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:
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 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.