Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions docs/csharp/fundamentals/types/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,13 @@ All enums inherit from <xref:System.Enum?displayProperty=nameWithType>, 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":::

Expand Down
16 changes: 12 additions & 4 deletions docs/csharp/programming-guide/types/snippets/index/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,22 @@ static void Declarations()
static void DeclarationAndAssignment()
{
// <DeclarationAndAssignment>
MyClass mc = new MyClass();
MyClass mc2 = mc;
MyClass myClass = new MyClass();
MyClass myClass2 = myClass;
// </DeclarationAndAssignment>
}

static void InterfaceAssignment()
{
// <InterfaceDeclaration>
IMyInterface iface = new MyClass();
// </InterfaceDeclaration>
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();
// </InterfaceDeclaration>
}

static void ArrayDeclaration()
Expand Down