Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions docs/csharp/language-reference/keywords/new-operator.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: "new Operator (C# Reference)"
ms.date: 07/20/2015
ms.date: 03/15/2018
ms.prod: .net
ms.technology:
- "devlang-csharp"
Expand All @@ -15,34 +15,34 @@ ms.author: "wiwagn"
# new Operator (C# Reference)
Used to create objects and invoke constructors. For example:

```
```csharp
Class1 obj = new Class1();
```

It is also used to create instances of anonymous types:

```
```csharp
var query = from cust in customers
select new {Name = cust.Name, Address = cust.PrimaryAddress};
select new { Name = cust.Name, Address = cust.PrimaryAddress };
```

The `new` operator is also used to invoke the default constructor for value types. For example:

```
```csharp
int i = new int();
```

In the preceding statement, `i` is initialized to `0`, which is the default value for the type `int`. The statement has the same effect as the following:

```
```csharp
int i = 0;
```

For a complete list of default values, see [Default Values Table](../../../csharp/language-reference/keywords/default-values-table.md).

Remember that it is an error to declare a default constructor for a [struct](../../../csharp/language-reference/keywords/struct.md) because every value type implicitly has a public default constructor. It is possible to declare parameterized constructors on a struct type to set its initial values, but this is only necessary if values other than the default are required.

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed. For reference types that consume fixed resources such as large amounts of memory, file handles, or network connections, it is sometimes desirable to employ deterministic finalization to ensure that the object is destroyed as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md).
Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when their containing context is destroyed, whereas reference-type objects are destroyed by the garbage collector at an unspecified time after the last reference to them is removed. For types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources they contain are released as soon as possible. For more information, see [using Statement](../../../csharp/language-reference/keywords/using-statement.md).

The `new` operator cannot be overloaded.

Expand All @@ -51,7 +51,7 @@ int i = 0;
## Example
In the following example, a `struct` object and a class object are created and initialized by using the `new` operator and then assigned values. The default and the assigned values are displayed.

[!code-csharp[csrefKeywordsOperator#7](../../../csharp/language-reference/keywords/codesnippet/CSharp/new-operator_1.cs)]
[!code-csharp[csrefKeywordsOperator#7](codesnippet/CSharp/new-operator_1.cs)]

Notice in the example that the default value of a string is `null`. Therefore, it is not displayed.

Expand Down