Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
C# Reference
C# Keywords
new (C# Reference)
 new Constraint

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
C# Language Reference
new Constraint (C# Reference)

The new constraint specifies that any type argument in a generic class declaration must have a public parameterless constructor. To use the new constraint, the type cannot be abstract.

Apply the new constraint to a type parameter when your generic class creates new instances of the type, as shown in the following example:

C#
class ItemFactory<T> where T : new()
{
    public T GetNewItem()
    {
        return new T();
    }
}


When you use the new() constraint with other constraints, it must be specified last:

C#
public class ItemFactory2<T>
    where T : IComparable, new()
{
}


For more information, see Constraints on Type Parameters (C# Programming Guide).

For more information, see the C# Language Specification.

  • 4.4.3 Satisfying constraints

  • 10.1.6 Type parameter constraints

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker