This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Thenew
constraint specifies that a type argument in a generic class or method declaration must have a public parameterless constructor. To use thenew
constraint, the type cannot be abstract.
Apply thenew
constraint to a type parameter when a generic class creates new instances of the type, as shown in the following example:
class ItemFactory<T> where T : new(){ public T GetNewItem() { return new T(); }}
When you use thenew()
constraint with other constraints, it must be specified last:
public class ItemFactory2<T> where T : IComparable, new(){ }
For more information, seeConstraints on Type Parameters.
You can also use thenew
keyword tocreate an instance of a type or as amember declaration modifier.
For more information, see theType parameter constraints section of theC# language specification.
Was this page helpful?
Was this page helpful?