Constraints list on MSDN.

Using example - create an instance of an object that must have a public parameterless constructor:

public class ObjectFactory<T> where T : new()
{
    public static T CreateObject()
    {
        return new T();
    }
}

Advantage: specifying a class that does not provide a default constructor will now generate a compile time error.

No Comments »