C# where keyword

it is a constraint for generics

MSDN

so the new() constraint says it must have a public parameterless constructor


It specifies a constraint on the generic type parameter T.

The new() constraint specifies that T must have a public default constructor.

You can also stipulate that the type must be a class (or conversely, a struct), that it must implement a given interface, or that it must derive from a particular class.


The where clause is used to specify constraints on the types that can be used as arguments for a type parameter defined in a generic declaration. For example, you can declare a generic class, MyGenericClass, such that the type parameter T implements the IComparable interface:

public class MyGenericClass<T> where T:IComparable { }

In this particular case it says that T must implement a default constructor.

Tags:

C#