new 运算符(C# 参考)new operator (C# reference)

new 运算符创建类型的新实例。The new operator creates a new instance of a type.

new 关键字还可用作成员声明修饰符泛型类型约束You can also use the new keyword as a member declaration modifier or a generic type constraint.

构造函数调用Constructor invocation

要创建类型的新实例,通常使用 new 运算符调用该类型的某个构造函数To create a new instance of a type, you typically invoke one of the constructors of that type using the new 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: 30

可以使用带有 new 运算符的对象或集合初始值设定项实例化和初始化一个语句中的对象,如下例所示: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:

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

从 C# 9.0 开始,构造调用表达式由目标确定类型。Beginning with C# 9.0, 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:

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 }

如前面的示例所示,在由目标确定类型的 new 表达式中始终使用括号。As the preceding example shows, you always use parentheses in a target-typed new expression.

如果 new 表达式的目标类型未知(例如使用 var 关键字时),必须指定类型名称。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

还可以使用 new 运算符创建数组实例,如下例所示:You also use the new 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, 30

使用数组初始化语法创建数组实例,并在一个语句中使用元素填充该实例。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:

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, see Arrays.

匿名类型的实例化Instantiation of anonymous types

要创建匿名类型的实例,请使用 new 运算符和对象初始值设定项语法:To create an instance of an anonymous type, use the new operator and object initializer syntax:

var example = new { Greeting = "Hello", Name = "World" };
Console.WriteLine($"{example.Greeting}, {example.Name}!");
// Output:
// Hello, World!

类型实例的析构Destruction of type instances

无需销毁此前创建的类型实例。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 the garbage 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. 有关详细信息,请参阅 System.IDisposable API 参考和 using 语句一文。For more information, see the System.IDisposable API reference and the using statement article.

运算符可重载性Operator overloadability

用户定义的类型不能重载 new 运算符。A user-defined type cannot overload the new operator.

C# 语言规范C# language specification

有关详细信息,请参阅 C# 语言规范new 运算符部分。For more information, see The new operator section of the C# language specification.

有关条件由目标确定类型的 new 表达式的详细信息,请参阅功能建议说明For more information about a target-typed new expression, see the feature proposal note.

另请参阅See also