From 208e2b995f5aa60aa8a17130083f8a44eefb49e0 Mon Sep 17 00:00:00 2001 From: Petr Onderka Date: Thu, 15 Mar 2018 17:29:07 +0100 Subject: [PATCH 1/5] Operator new doesn't create structs on the stack --- docs/csharp/language-reference/keywords/new-operator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/keywords/new-operator.md b/docs/csharp/language-reference/keywords/new-operator.md index 7e39b1c16e31a..d96bd882bb0a0 100644 --- a/docs/csharp/language-reference/keywords/new-operator.md +++ b/docs/csharp/language-reference/keywords/new-operator.md @@ -42,7 +42,7 @@ int i = 0; 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 they go out of scope, whereas reference-type objects are destroyed at an unspecified time after the last reference to them is removed. For reference types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources it contains 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. From fd6cdc0912b14c89cfdde8771f9ea02fe677e1b5 Mon Sep 17 00:00:00 2001 From: Petr Onderka Date: Thu, 15 Mar 2018 17:34:29 +0100 Subject: [PATCH 2/5] Small improvements to operator new --- .../language-reference/keywords/new-operator.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/csharp/language-reference/keywords/new-operator.md b/docs/csharp/language-reference/keywords/new-operator.md index d96bd882bb0a0..76567f76637e5 100644 --- a/docs/csharp/language-reference/keywords/new-operator.md +++ b/docs/csharp/language-reference/keywords/new-operator.md @@ -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" @@ -15,26 +15,26 @@ 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; ``` @@ -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. From 8671934a00261fa79452d2f89c832244cb5e3442 Mon Sep 17 00:00:00 2001 From: Petr Onderka Date: Thu, 15 Mar 2018 19:23:28 +0100 Subject: [PATCH 3/5] Structs can contain resources too --- docs/csharp/language-reference/keywords/new-operator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/keywords/new-operator.md b/docs/csharp/language-reference/keywords/new-operator.md index 76567f76637e5..e9517c76b6380 100644 --- a/docs/csharp/language-reference/keywords/new-operator.md +++ b/docs/csharp/language-reference/keywords/new-operator.md @@ -42,7 +42,7 @@ int i = 0; 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. - Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, whereas reference-type objects are destroyed at an unspecified time after the last reference to them is removed. For reference types that contain resources such as file handles, or network connections, it is desirable to employ deterministic cleanup to ensure that the resources it contains are released 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 they go out of scope, whereas reference-type objects are destroyed 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. From 8d280767aa6a603c56fecb03b51920ead4eea369 Mon Sep 17 00:00:00 2001 From: Petr Onderka Date: Fri, 16 Mar 2018 14:06:12 +0100 Subject: [PATCH 4/5] Mention garbage collector --- docs/csharp/language-reference/keywords/new-operator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/keywords/new-operator.md b/docs/csharp/language-reference/keywords/new-operator.md index e9517c76b6380..c1eebf7dfaab0 100644 --- a/docs/csharp/language-reference/keywords/new-operator.md +++ b/docs/csharp/language-reference/keywords/new-operator.md @@ -42,7 +42,7 @@ int i = 0; 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. - Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, whereas reference-type objects are destroyed 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). + Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, 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. From 000e7671b60f375caa298a81ad1f533510dfdf30 Mon Sep 17 00:00:00 2001 From: Petr Onderka Date: Sat, 17 Mar 2018 18:53:32 +0100 Subject: [PATCH 5/5] Clarified when value types are destroyed --- docs/csharp/language-reference/keywords/new-operator.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/language-reference/keywords/new-operator.md b/docs/csharp/language-reference/keywords/new-operator.md index c1eebf7dfaab0..7057f23805ef4 100644 --- a/docs/csharp/language-reference/keywords/new-operator.md +++ b/docs/csharp/language-reference/keywords/new-operator.md @@ -42,7 +42,7 @@ int i = 0; 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. - Both value-type objects such as structs and reference-type objects such as classes are destroyed automatically, but value-type objects are destroyed when they go out of scope, 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). + 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.