January 27, 2010

Generic Constraints Syntax In C# (using where keyword)

On MSDN
or
Search for MSDN page with this

Takes the form:
... class SomeClass<args>
     where Args : class

Here is a list of valid constraints

where T: struct
The type argument must be a value type. Any value type except Nullable can be specified.

where T : class
The type argument must be a reference type; this applies also to any class, interface, delegate, or array type.

where T : new()
The type argument must have a public parameterless constructor. When used together with other constraints, the new() constraint must be specified last.

where T : <base class name>
The type argument must be or derive from the specified base class.

where T : <interface name>
The type argument must be or implement the specified interface. Can be more than one and the interfaces specified can be generic

where T : U
The type argument supplied for T must be or derive from the argument supplied for U.

No comments: