Skip to content

Commit 456fd6e

Browse files
authored
Rework the explanation of reference types / interfaces on the C# type system overview. (#26419)
* 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. * Missed one name change for DeclarationAndAssignment example. * Move interface examples to own method to avoid duplicate name conflict. * Update wording per review comments
1 parent 6a8ee36 commit 456fd6e

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

docs/csharp/fundamentals/types/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,13 @@ All enums inherit from <xref:System.Enum?displayProperty=nameWithType>, which in
114114

115115
### Reference types
116116

117-
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:
117+
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).
118+
119+
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:
118120

119121
:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="DeclarationAndAssignment":::
120122

121-
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:
123+
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:
122124

123125
:::code language="csharp" source="../../programming-guide/types/snippets/index/Program.cs" ID="InterfaceDeclaration":::
124126

docs/csharp/programming-guide/types/snippets/index/Program.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,22 @@ static void Declarations()
132132
static void DeclarationAndAssignment()
133133
{
134134
// <DeclarationAndAssignment>
135-
MyClass mc = new MyClass();
136-
MyClass mc2 = mc;
135+
MyClass myClass = new MyClass();
136+
MyClass myClass2 = myClass;
137137
// </DeclarationAndAssignment>
138+
}
138139

140+
static void InterfaceAssignment()
141+
{
139142
// <InterfaceDeclaration>
140-
IMyInterface iface = new MyClass();
141-
// </InterfaceDeclaration>
143+
MyClass myClass = new MyClass();
142144

145+
// Declare and assign using an existing value.
146+
IMyInterface myInterface = myClass;
147+
148+
// Or create and assign a value in a single statement.
149+
IMyInterface myInterface2 = new MyClass();
150+
// </InterfaceDeclaration>
143151
}
144152

145153
static void ArrayDeclaration()

0 commit comments

Comments
 (0)