Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2005
Visual Studio
Visual C#
C# Reference
C# Keywords
Operator Keywords
new
 new Constraint
Collapse All/Expand All Collapse All
This page is specific to
Microsoft Visual Studio 2005/.NET Framework 2.0

Other versions are also available for the following:

Want more? Here are some additional resources on this topic:

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. Apply this constraint to a type parameter when your 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 the new() constraint with other constraints, it must be specified last:

using System;
public class ItemFactory<T>
    where T : IComparable, new()
{
}

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

For more information, see the following sections in the C# Language Specification:

  • 19.1.4 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 | Site Feedback
Page view tracker