C# new operator
new operator
The new
operator
creates a new instance of a type.
You
can also use the new
keyword
as a memberdeclaration modifier or a generictype constraint.
Constructor Invocation
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
You
can use an objector 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> { [] = 10, [] = 20, [] = 30 }; Console.WriteLine(string.Join("; ", dict.Select(entry => $"{entry.Key}: {entry.Value}"))); // Output: // first: 10; second: 20; third: 30
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() { [] = new() { 1, 2, 3 }, [] = new() { 5, 8, 3 }, [] = new() { 1, 0, 4 } };
As
the preceding example shows, you 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:
var numbers = new int[3]; numbers[0] = 10; numbers[1] = 20; numbers[2] = 30; Console.WriteLine(string.Join(", ", numbers)); // Output: // 10, 20, 30
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:
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. For more information, see the System.IDisposable API reference and the using statement article.
Operator overloadability
A
user-defined type cannot overload the new
operator.
C# language specification
For more information, see The new operator section of the C# language specification.
No comments:
Post a Comment