C#idiomatic C#
new-operatorcsharp/language-reference/operators

Language

new operator - The `new` operator creates a new instance of a type

The C# new operator is used to create a optionally initialize a new instance of a type.

  • page csharp/language-reference/operators/new-operator
  • updated 01/20/2026

The new operator creates a new instance of a type. You can also use the new keyword as a member declaration modifier or a generic type constraint.


ms.custom: "updateeachrelease"

The C# language reference documents the most recently released version of the C# language. It also contains initial documentation for features in public previews for the upcoming language release.

The documentation identifies any feature first introduced in the last three versions of the language or in current public previews.

Constructor invocation

To create a new instance of a type, invoke one of the constructors of that type by using the new operator:

sample · csharpdocs/csharp/language-reference/operators/snippets/shared/NewOperator.csConstructor

csharp
namespace operators;

public static class NewOperator
{
    public static void Examples()
    {
        Constructor();
        ConstructorWithInitializer();
        Array();
        ArrayInitialization();
        AnonymousType();
    }

    private static void Constructor()
    {
        // <SnippetConstructor>
        var dict = new Dictionary<string, int>();
        dict["first"] = 10;
        dict["second"] = 20;
        dict["third"] = 30;

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructor>
    }

    private static void ConstructorWithInitializer()
    {
        // <SnippetConstructorWithInitializer>
        var dict = new Dictionary<string, int>
        {
            ["first"] = 10,
            ["second"] = 20,
            ["third"] = 30
        };

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructorWithInitializer>
    }

    private static void TargetTyped()
    {
        // <SnippetTargetTyped>
        List<int> xs = new();
        List<int> ys = new(capacity: 10_000);
        List<int> zs = new() { Capacity = 20_000 };

        Dictionary<int, List<int>> lookup = new()
        {
            [1] = new() { 1, 2, 3 },
            [2] = new() { 5, 8, 3 },
            [5] = new() { 1, 0, 4 }
        };
        // </SnippetTargetTyped>
    }

    private static void Array()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArray>
        var numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        Console.WriteLine(string.Join(", ", numbers));
        // Output:
        // 10, 20, 30
        // </SnippetArray>
    }

    private static void ArrayInitialization()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArrayInitialization>
        var a = new int[3] { 10, 20, 30 };
        var b = new int[] { 10, 20, 30 };
        var c = new[] { 10, 20, 30 };
        Console.WriteLine(c.GetType());  // output: System.Int32[]
        // </SnippetArrayInitialization>
    }

    private static void AnonymousType()
    {
        // <SnippetAnonymousType>
        var example = new { Greeting = "Hello", Name = "World" };
        Console.WriteLine($"{example.Greeting}, {example.Name}!");
        // Output:
        // Hello, World!
        // </SnippetAnonymousType>
    }
}

You can use an object or collection initializer with the new operator to instantiate and initialize an object in one statement, as the following example shows:

sample · csharpdocs/csharp/language-reference/operators/snippets/shared/NewOperator.csConstructorWithInitializer

csharp
namespace operators;

public static class NewOperator
{
    public static void Examples()
    {
        Constructor();
        ConstructorWithInitializer();
        Array();
        ArrayInitialization();
        AnonymousType();
    }

    private static void Constructor()
    {
        // <SnippetConstructor>
        var dict = new Dictionary<string, int>();
        dict["first"] = 10;
        dict["second"] = 20;
        dict["third"] = 30;

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructor>
    }

    private static void ConstructorWithInitializer()
    {
        // <SnippetConstructorWithInitializer>
        var dict = new Dictionary<string, int>
        {
            ["first"] = 10,
            ["second"] = 20,
            ["third"] = 30
        };

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructorWithInitializer>
    }

    private static void TargetTyped()
    {
        // <SnippetTargetTyped>
        List<int> xs = new();
        List<int> ys = new(capacity: 10_000);
        List<int> zs = new() { Capacity = 20_000 };

        Dictionary<int, List<int>> lookup = new()
        {
            [1] = new() { 1, 2, 3 },
            [2] = new() { 5, 8, 3 },
            [5] = new() { 1, 0, 4 }
        };
        // </SnippetTargetTyped>
    }

    private static void Array()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArray>
        var numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        Console.WriteLine(string.Join(", ", numbers));
        // Output:
        // 10, 20, 30
        // </SnippetArray>
    }

    private static void ArrayInitialization()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArrayInitialization>
        var a = new int[3] { 10, 20, 30 };
        var b = new int[] { 10, 20, 30 };
        var c = new[] { 10, 20, 30 };
        Console.WriteLine(c.GetType());  // output: System.Int32[]
        // </SnippetArrayInitialization>
    }

    private static void AnonymousType()
    {
        // <SnippetAnonymousType>
        var example = new { Greeting = "Hello", Name = "World" };
        Console.WriteLine($"{example.Greeting}, {example.Name}!");
        // Output:
        // Hello, World!
        // </SnippetAnonymousType>
    }
}

Target-typed new

Constructor invocation expressions are target-typed. That is, if a target type of an expression is known, you can omit a type name, as the following example shows:

sample · csharpdocs/csharp/language-reference/operators/snippets/shared/NewOperator.csSnippetTargetTyped

csharp
namespace operators;

public static class NewOperator
{
    public static void Examples()
    {
        Constructor();
        ConstructorWithInitializer();
        Array();
        ArrayInitialization();
        AnonymousType();
    }

    private static void Constructor()
    {
        // <SnippetConstructor>
        var dict = new Dictionary<string, int>();
        dict["first"] = 10;
        dict["second"] = 20;
        dict["third"] = 30;

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructor>
    }

    private static void ConstructorWithInitializer()
    {
        // <SnippetConstructorWithInitializer>
        var dict = new Dictionary<string, int>
        {
            ["first"] = 10,
            ["second"] = 20,
            ["third"] = 30
        };

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructorWithInitializer>
    }

    private static void TargetTyped()
    {
        // <SnippetTargetTyped>
        List<int> xs = new();
        List<int> ys = new(capacity: 10_000);
        List<int> zs = new() { Capacity = 20_000 };

        Dictionary<int, List<int>> lookup = new()
        {
            [1] = new() { 1, 2, 3 },
            [2] = new() { 5, 8, 3 },
            [5] = new() { 1, 0, 4 }
        };
        // </SnippetTargetTyped>
    }

    private static void Array()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArray>
        var numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        Console.WriteLine(string.Join(", ", numbers));
        // Output:
        // 10, 20, 30
        // </SnippetArray>
    }

    private static void ArrayInitialization()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArrayInitialization>
        var a = new int[3] { 10, 20, 30 };
        var b = new int[] { 10, 20, 30 };
        var c = new[] { 10, 20, 30 };
        Console.WriteLine(c.GetType());  // output: System.Int32[]
        // </SnippetArrayInitialization>
    }

    private static void AnonymousType()
    {
        // <SnippetAnonymousType>
        var example = new { Greeting = "Hello", Name = "World" };
        Console.WriteLine($"{example.Greeting}, {example.Name}!");
        // Output:
        // Hello, World!
        // </SnippetAnonymousType>
    }
}

As the preceding example shows, always use parentheses in a target-typed new expression.

If a target type of a new expression is unknown (for example, when you use the var keyword), you must specify a type name.

Array creation

You also use the new operator to create an array instance, as the following example shows:

sample · csharpdocs/csharp/language-reference/operators/snippets/shared/NewOperator.csArray

csharp
namespace operators;

public static class NewOperator
{
    public static void Examples()
    {
        Constructor();
        ConstructorWithInitializer();
        Array();
        ArrayInitialization();
        AnonymousType();
    }

    private static void Constructor()
    {
        // <SnippetConstructor>
        var dict = new Dictionary<string, int>();
        dict["first"] = 10;
        dict["second"] = 20;
        dict["third"] = 30;

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructor>
    }

    private static void ConstructorWithInitializer()
    {
        // <SnippetConstructorWithInitializer>
        var dict = new Dictionary<string, int>
        {
            ["first"] = 10,
            ["second"] = 20,
            ["third"] = 30
        };

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructorWithInitializer>
    }

    private static void TargetTyped()
    {
        // <SnippetTargetTyped>
        List<int> xs = new();
        List<int> ys = new(capacity: 10_000);
        List<int> zs = new() { Capacity = 20_000 };

        Dictionary<int, List<int>> lookup = new()
        {
            [1] = new() { 1, 2, 3 },
            [2] = new() { 5, 8, 3 },
            [5] = new() { 1, 0, 4 }
        };
        // </SnippetTargetTyped>
    }

    private static void Array()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArray>
        var numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        Console.WriteLine(string.Join(", ", numbers));
        // Output:
        // 10, 20, 30
        // </SnippetArray>
    }

    private static void ArrayInitialization()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArrayInitialization>
        var a = new int[3] { 10, 20, 30 };
        var b = new int[] { 10, 20, 30 };
        var c = new[] { 10, 20, 30 };
        Console.WriteLine(c.GetType());  // output: System.Int32[]
        // </SnippetArrayInitialization>
    }

    private static void AnonymousType()
    {
        // <SnippetAnonymousType>
        var example = new { Greeting = "Hello", Name = "World" };
        Console.WriteLine($"{example.Greeting}, {example.Name}!");
        // Output:
        // Hello, World!
        // </SnippetAnonymousType>
    }
}

Use array initialization syntax to create an array instance and populate it with elements in one statement. The following example shows various ways how you can do that:

sample · csharpdocs/csharp/language-reference/operators/snippets/shared/NewOperator.csArrayInitialization

csharp
namespace operators;

public static class NewOperator
{
    public static void Examples()
    {
        Constructor();
        ConstructorWithInitializer();
        Array();
        ArrayInitialization();
        AnonymousType();
    }

    private static void Constructor()
    {
        // <SnippetConstructor>
        var dict = new Dictionary<string, int>();
        dict["first"] = 10;
        dict["second"] = 20;
        dict["third"] = 30;

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructor>
    }

    private static void ConstructorWithInitializer()
    {
        // <SnippetConstructorWithInitializer>
        var dict = new Dictionary<string, int>
        {
            ["first"] = 10,
            ["second"] = 20,
            ["third"] = 30
        };

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructorWithInitializer>
    }

    private static void TargetTyped()
    {
        // <SnippetTargetTyped>
        List<int> xs = new();
        List<int> ys = new(capacity: 10_000);
        List<int> zs = new() { Capacity = 20_000 };

        Dictionary<int, List<int>> lookup = new()
        {
            [1] = new() { 1, 2, 3 },
            [2] = new() { 5, 8, 3 },
            [5] = new() { 1, 0, 4 }
        };
        // </SnippetTargetTyped>
    }

    private static void Array()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArray>
        var numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        Console.WriteLine(string.Join(", ", numbers));
        // Output:
        // 10, 20, 30
        // </SnippetArray>
    }

    private static void ArrayInitialization()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArrayInitialization>
        var a = new int[3] { 10, 20, 30 };
        var b = new int[] { 10, 20, 30 };
        var c = new[] { 10, 20, 30 };
        Console.WriteLine(c.GetType());  // output: System.Int32[]
        // </SnippetArrayInitialization>
    }

    private static void AnonymousType()
    {
        // <SnippetAnonymousType>
        var example = new { Greeting = "Hello", Name = "World" };
        Console.WriteLine($"{example.Greeting}, {example.Name}!");
        // Output:
        // Hello, World!
        // </SnippetAnonymousType>
    }
}

For more information about arrays, see Arrays.

Instantiation of anonymous types

To create an instance of an anonymous type, use the new operator and object initializer syntax:

sample · csharpdocs/csharp/language-reference/operators/snippets/shared/NewOperator.csAnonymousType

csharp
namespace operators;

public static class NewOperator
{
    public static void Examples()
    {
        Constructor();
        ConstructorWithInitializer();
        Array();
        ArrayInitialization();
        AnonymousType();
    }

    private static void Constructor()
    {
        // <SnippetConstructor>
        var dict = new Dictionary<string, int>();
        dict["first"] = 10;
        dict["second"] = 20;
        dict["third"] = 30;

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructor>
    }

    private static void ConstructorWithInitializer()
    {
        // <SnippetConstructorWithInitializer>
        var dict = new Dictionary<string, int>
        {
            ["first"] = 10,
            ["second"] = 20,
            ["third"] = 30
        };

        Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}")));
        // Output:
        // first: 10; second: 20; third: 30
        // </SnippetConstructorWithInitializer>
    }

    private static void TargetTyped()
    {
        // <SnippetTargetTyped>
        List<int> xs = new();
        List<int> ys = new(capacity: 10_000);
        List<int> zs = new() { Capacity = 20_000 };

        Dictionary<int, List<int>> lookup = new()
        {
            [1] = new() { 1, 2, 3 },
            [2] = new() { 5, 8, 3 },
            [5] = new() { 1, 0, 4 }
        };
        // </SnippetTargetTyped>
    }

    private static void Array()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArray>
        var numbers = new int[3];
        numbers[0] = 10;
        numbers[1] = 20;
        numbers[2] = 30;
        Console.WriteLine(string.Join(", ", numbers));
        // Output:
        // 10, 20, 30
        // </SnippetArray>
    }

    private static void ArrayInitialization()
    {
        // This snippet is used with try.net,
        // which doesn't support collection expressions yet.
        // <SnippetArrayInitialization>
        var a = new int[3] { 10, 20, 30 };
        var b = new int[] { 10, 20, 30 };
        var c = new[] { 10, 20, 30 };
        Console.WriteLine(c.GetType());  // output: System.Int32[]
        // </SnippetArrayInitialization>
    }

    private static void AnonymousType()
    {
        // <SnippetAnonymousType>
        var example = new { Greeting = "Hello", Name = "World" };
        Console.WriteLine($"{example.Greeting}, {example.Name}!");
        // Output:
        // Hello, World!
        // </SnippetAnonymousType>
    }
}

Destruction of type instances

You don't need to destroy previously created type instances. The system automatically destroys instances of both reference and value types. The system destroys instances of value types as soon as the context that contains them is destroyed. The garbage collector destroys instances of reference types at some unspecified time after the last reference to them is removed.

For type instances that contain unmanaged resources, such as a file handle, employ deterministic clean-up to ensure that the resources are released as soon as possible. For more information, see the System.IDisposable API reference and the using statement article.

Operator overloadability

A user-defined type can't overload the new operator.

C# language specification

For more information, see The new operator section of the C# language specification.

For more information about a target-typed new expression, see the Object creation expressions section of the C# language specification.

See also