diff --git a/docs/csharp/fundamentals/types/index.md b/docs/csharp/fundamentals/types/index.md index 64a7d4e1709b7..fc5ed76df9976 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 that 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..9d089d0783ace 100644 --- a/docs/csharp/programming-guide/types/snippets/index/Program.cs +++ b/docs/csharp/programming-guide/types/snippets/index/Program.cs @@ -132,14 +132,22 @@ static void Declarations() static void DeclarationAndAssignment() { // - MyClass mc = new MyClass(); - MyClass mc2 = mc; + MyClass myClass = new MyClass(); + MyClass myClass2 = myClass; // + } + static void InterfaceAssignment() + { // - 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(); + // } static void ArrayDeclaration()