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.
new operator creates a new instance of a typeThenew operator creates a new instance of a type. You can also use thenew keyword as amember declaration modifier or ageneric type constraint.
To create a new instance of a type, you typically invoke one of theconstructors of that type using thenew operator:
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: 30You can use anobject or collection initializer with thenew operator to instantiate and initialize an object in one statement, as the following example shows:
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: 30newConstructor 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:
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 }};As the preceding example shows, you always use parentheses in a target-typednew expression.
If a target type of anew expression is unknown (for example, when you use thevar keyword), you must specify a type name.
You also use thenew operator to create an array instance, as the following example shows:
var numbers = new int[3];numbers[0] = 10;numbers[1] = 20;numbers[2] = 30;Console.WriteLine(string.Join(", ", numbers));// Output:// 10, 20, 30Use 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:
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[]For more information about arrays, seeArrays.
To create an instance of ananonymous type, use thenew operator and object initializer syntax:
var example = new { Greeting = "Hello", Name = "World" };Console.WriteLine($"{example.Greeting}, {example.Name}!");// Output:// Hello, World!You don't have to destroy earlier created type instances. Instances of both reference and value types are destroyed automatically. Instances of value types are destroyed as soon as the context that contains them is destroyed. Instances of reference types are destroyed by thegarbage collector at some unspecified time after the last reference to them is removed.
For type instances that contain unmanaged resources, for example, a file handle, it's recommended to employ deterministic clean-up to ensure that the resources they contain are released as soon as possible. For more information, see theSystem.IDisposable API reference and theusing statement article.
A user-defined type can't overload thenew operator.
For more information, seeThe new operator section of theC# language specification.
For more information about a target-typednew expression, see thefeature proposal note.
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?