2

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.

Community
  • 1
  • 1
Andrey
  • 20,487
  • 26
  • 108
  • 176

6 Answers6

6

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

Tim
  • 14,999
  • 1
  • 45
  • 68
1

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.

Bala R
  • 107,317
  • 23
  • 199
  • 210
1

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).

agent-j
  • 27,335
  • 5
  • 52
  • 79
1

It means that the type TEnum must be able to use

var x = new TEnum();

zbugs
  • 591
  • 2
  • 10
1

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.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
1

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.

KeithS
  • 70,210
  • 21
  • 112
  • 164