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
  • 19,434
  • 24
  • 100
  • 171

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,691
  • 1
  • 42
  • 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
  • 104,615
  • 23
  • 192
  • 207
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
  • 26,389
  • 5
  • 49
  • 78
1

It means that the type TEnum must be able to use

var x = new TEnum();

zbugs
  • 561
  • 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
  • 539,124
  • 75
  • 1,126
  • 1,354
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
  • 68,043
  • 20
  • 107
  • 163