From 5b2cf454f6359f42537d9c6d0f3736a9ab1ee379 Mon Sep 17 00:00:00 2001 From: forensicmike1 Date: Wed, 6 Oct 2021 14:43:48 -0600 Subject: [PATCH 1/4] Rework the explanation of reference types / interfaces on the C# type system overview. - Add link to reference types page - Update associated code sample to use clearer variable names. --- docs/csharp/fundamentals/types/index.md | 6 ++++-- .../types/snippets/index/Program.cs | 12 +++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 64a7d4e1709b7..990cf1dbdcd43 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -114,11 +114,13 @@ All enums inherit from , which in ### Reference types -A type that is defined as a `class`, `record`, [`delegate`](../../language-reference/builtin-types/reference-types.md), array, or [`interface`](../../language-reference/keywords/interface.md) is a *reference type*. At run time, when you declare a variable of a reference type, the variable contains the value [`null`](../../language-reference/keywords/null.md) until you explicitly create an object by using the [`new`](../../language-reference/operators/new-operator.md) operator, or assign it an object that has been created elsewhere by using `new`, as shown in the following example: +A type that is defined as a `class`, `record`, [`delegate`](../../language-reference/builtin-types/reference-types.md), array, or [`interface`](../../language-reference/keywords/interface.md) is a [`reference type`](../../language-reference/keywords/reference-types.md). + +When declaring a variable of a [`reference type`](../../language-reference/keywords/reference-types.md), it contains the value [`null`](../../language-reference/keywords/null.md) until you assign it with an instance of that type or create one using the [`new`](../../language-reference/operators/new-operator.md) operator. Creation and assignment of a class are demonstrated in the following example: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="DeclarationAndAssignment"::: -An interface must be initialized together with a class object that implements it. If `MyClass` implements `IMyInterface`, you create an instance of `IMyInterface` as shown in the following example: +An [`interface`](../../language-reference/keywords/interface.md) cannot be directly instantiated using the [`new`](../../language-reference/operators/new-operator.md) operator. Instead, create and assign an instance of a class who implements the interface. Consider the following example: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="InterfaceDeclaration"::: diff --git a/docs/csharp/programming-guide/types/snippets/index/Program.cs b/docs/csharp/programming-guide/types/snippets/index/Program.cs index 170e04adba8eb..ffc10bd34f01b 100644 --- a/docs/csharp/programming-guide/types/snippets/index/Program.cs +++ b/docs/csharp/programming-guide/types/snippets/index/Program.cs @@ -132,12 +132,18 @@ static void Declarations() static void DeclarationAndAssignment() { // - MyClass mc = new MyClass(); - MyClass mc2 = mc; + MyClass myClass = new MyClass(); + MyClass myClass2 = mc; // // - IMyInterface iface = new MyClass(); + MyClass myClass = new MyClass(); + + // Declare and assign using an existing value. + IMyInterface myInterface = myClass; + + // Or create and assign a value in a single statement. + IMyInterface myInterface2 = new MyClass(); // } From ce89af9317ad3bdb9a35f7c4f2ead00f89e548be Mon Sep 17 00:00:00 2001 From: forensicmike1 Date: Wed, 6 Oct 2021 14:48:32 -0600 Subject: [PATCH 2/4] Missed one name change for DeclarationAndAssignment example. --- docs/csharp/programming-guide/types/snippets/index/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/programming-guide/types/snippets/index/Program.cs b/docs/csharp/programming-guide/types/snippets/index/Program.cs index ffc10bd34f01b..4b7783fe46b5f 100644 --- a/docs/csharp/programming-guide/types/snippets/index/Program.cs +++ b/docs/csharp/programming-guide/types/snippets/index/Program.cs @@ -133,7 +133,7 @@ static void DeclarationAndAssignment() { // MyClass myClass = new MyClass(); - MyClass myClass2 = mc; + MyClass myClass2 = myClass; // // From bdf75e418d9e134abf48061492a16a11cfc0f463 Mon Sep 17 00:00:00 2001 From: forensicmike1 Date: Wed, 6 Oct 2021 14:54:48 -0600 Subject: [PATCH 3/4] Move interface examples to own method to avoid duplicate name conflict. --- docs/csharp/programming-guide/types/snippets/index/Program.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/csharp/programming-guide/types/snippets/index/Program.cs b/docs/csharp/programming-guide/types/snippets/index/Program.cs index 4b7783fe46b5f..9d089d0783ace 100644 --- a/docs/csharp/programming-guide/types/snippets/index/Program.cs +++ b/docs/csharp/programming-guide/types/snippets/index/Program.cs @@ -135,7 +135,10 @@ static void DeclarationAndAssignment() MyClass myClass = new MyClass(); MyClass myClass2 = myClass; // + } + static void InterfaceAssignment() + { // MyClass myClass = new MyClass(); @@ -145,7 +148,6 @@ static void DeclarationAndAssignment() // Or create and assign a value in a single statement. IMyInterface myInterface2 = new MyClass(); // - } static void ArrayDeclaration() From 09163575eec09a206745bc67b5401d495c715a97 Mon Sep 17 00:00:00 2001 From: forensicmike1 Date: Thu, 7 Oct 2021 11:01:17 -0600 Subject: [PATCH 4/4] Update wording per review comments --- docs/csharp/fundamentals/types/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 990cf1dbdcd43..fc5ed76df9976 100644 --- a/docs/csharp/fundamentals/types/index.md +++ b/docs/csharp/fundamentals/types/index.md @@ -120,7 +120,7 @@ When declaring a variable of a [`reference type`](../../language-reference/keywo :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="DeclarationAndAssignment"::: -An [`interface`](../../language-reference/keywords/interface.md) cannot be directly instantiated using the [`new`](../../language-reference/operators/new-operator.md) operator. Instead, create and assign an instance of a class who implements the interface. Consider the following example: +An [`interface`](../../language-reference/keywords/interface.md) cannot be directly instantiated using the [`new`](../../language-reference/operators/new-operator.md) operator. Instead, create and assign an instance of a class that implements the interface. Consider the following example: :::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="InterfaceDeclaration":::