You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add information on default interface members - added in C# 8.0 (#16737)
* update warnings for DIM
For this commit, I checked all language reference articles that referred to an "interface". Those were all checked for any necessary updates.
The main "interface" article needs more work, and will be in the next commit.
* add information on default interface members
Interfaces can now contain implementations of some member types.
- Include information on default interface members and what new member types are allowed in interfaces.
- Rewrite the opening to match the new uses of interfaces.
- Refer to the correct section of the language standard.
- Add a reference to the C# 8.0 default interface members specification.
* fix build warning
* Update static description
Interfaces may now declare static methods.
Fixes#16061
* Acrolinx recommendations
* Clarify property declaration
The same syntax for auto implemented properties declares a property without an implementation in an interface.
See dotnet/samples#1917 (comment)
* Apply suggestions from code review
Co-Authored-By: Youssef Victor <[email protected]>
Co-Authored-By: Genevieve Warren <[email protected]>
* code fence static
Except in title links, and the opening sentence, where the intent is clearly not as the keyword, but as the concept of static.
Co-authored-by: Youssef Victor <[email protected]>
Co-authored-by: Genevieve Warren <[email protected]>
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/compiler-messages/cs0106.md
-3Lines changed: 0 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,6 @@ The modifier 'modifier' is not valid for this item
13
13
14
14
A class or interface member was marked with an invalid access modifier. The following examples describe some of these invalid modifiers:
15
15
16
-
- The [static](../keywords/static.md) and [public](../keywords/public.md) modifiers are not permitted on interface methods.
17
-
18
16
- The [static](../keywords/static.md) modifier is not permitted on a [local function](../../programming-guide/classes-and-structs/local-functions.md).
19
17
20
18
- The `public` keyword is not allowed on an explicit interface declaration. In this case, remove the `public` keyword from the explicit interface declaration.
Copy file name to clipboardExpand all lines: docs/csharp/language-reference/keywords/interface.md
+29-11Lines changed: 29 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,39 +1,57 @@
1
1
---
2
2
title: "interface - C# Reference"
3
-
ms.date: 07/20/2015
3
+
ms.date: 01/17/2020
4
4
f1_keywords:
5
5
- "interface_CSharpKeyword"
6
6
helpviewer_keywords:
7
7
- "interface keyword [C#]"
8
8
ms.assetid: 7da38e81-4f99-4bc5-b07d-c986b687eeba
9
9
---
10
-
# interface (C# Reference)
10
+
# :::no-loc text="interface"::: (C# Reference)
11
11
12
-
An interface contains only the signatures of [methods](../../programming-guide/classes-and-structs/methods.md), [properties](../../programming-guide/classes-and-structs/properties.md), [events](../../programming-guide/events/index.md) or [indexers](../../programming-guide/indexers/index.md). A class or struct that implements the interface must implement the members of the interface that are specified in the interface definition. In the following example, class `ImplementationClass` must implement a method named `SampleMethod` that has no parameters and returns `void`.
12
+
An interface defines a contract. Any [`class`](class.md) or [`struct`](struct.md) that implements that contract must provide an implementation of the members defined in the interface. Beginning with C# 8.0, an interface may define a default implementation for members. It may also define [`static`](static.md) members in order to provide a single implementation for common functionality.
13
+
14
+
In the following example, class `ImplementationClass` must implement a method named `SampleMethod` that has no parameters and returns `void`.
13
15
14
16
For more information and examples, see [Interfaces](../../programming-guide/interfaces/index.md).
An interface can be a member of a namespace or a class and can contain signatures of the following members:
22
+
An interface can be a member of a namespace or a class. An interface declaration can contain declarations (signatures without any implementation) of the following members:
An interface can inherit from one or more base interfaces.
29
+
These preceding member declarations typically do not contain a body. Beginning with C# 8.0, an interface member may declare a body. This is called a *default implementation*. Members with bodies permit the interface to provide a "default" implementation for classes and structs that don't provide an overriding implementation. In addition, beginning with C# 8.0, an interface may include:
-[Static fields, methods, properties, indexers, and events](static.md)
36
+
- Member declarations using the explicit interface implementation syntax.
37
+
- Explicit access modifiers (the default access is [`public`](access-modifiers.md)).
38
+
39
+
Interfaces may not contain instance state. While static fields are now permitted, instance fields are not permitted in interfaces. [Instance auto-properties](../../programming-guide/classes-and-structs/auto-implemented-properties.md) are not supported in interfaces, as they would implicitly declare a hidden field. This rule has a subtle effect on property declarations. In an interface declaration, the following code does not declare an auto-implemented property as it does in a `class` or `struct`. Instead, it declares a property that doesn't have a default implementation but must be implemented in any type that implements the interface:
40
+
41
+
```csharp
42
+
publicinterfaceINamed
43
+
{
44
+
publicstringName {get; set;}
45
+
}
46
+
```
47
+
48
+
An interface can inherit from one or more base interfaces. When an interface [overrides a method](override.md) implemented in a base interface, it must use the [explicit interface implementation](../../programming-guide/interfaces/explicit-interface-implementation.md) syntax.
31
49
32
50
When a base type list contains a base class and interfaces, the base class must come first in the list.
33
51
34
-
A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface.
52
+
A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface. In addtion, default interface members can only be accessed through an instance of the interface.
35
53
36
-
For more details and code examples on explicit interface implementation, see [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md).
54
+
For more information about explicit interface implementation, see [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md).
37
55
38
56
## Example
39
57
@@ -43,7 +61,7 @@ The following example demonstrates interface implementation. In this example, th
For more information, see the [Interfaces](~/_csharplang/spec/interfaces.md) section of the [C# language specification](~/_csharplang/spec/introduction.md) and the feature specification for [Default interface members - C# 8.0](~/_csharplang/proposals/csharp-8.0/default-interface-methods.md)
Use the `static` modifier to declare a static member, which belongs to the type itself rather than to a specific object. The `static` modifier can be used with classes, fields, methods, properties, operators, events, and constructors, but it cannot be used with indexers, finalizers, or types other than classes. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md).
13
+
Use the `static` modifier to declare a static member, which belongs to the type itself rather than to a specific object. The `static` modifier can be used to declare `static`classes. In classes, interfaces, and structs, you may add the `static` modifier to fields, methods, properties, operators, events, and constructors. The `static` modifier can't be used with indexersor finalizers. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md).
14
14
15
15
## Example
16
16
17
17
The following class is declared as `static` and contains only `static` methods:
A constant or type declaration is implicitly a static member.
22
-
23
-
A static member cannot be referenced through an instance. Instead, it is referenced through the type name. For example, consider the following class:
21
+
A constant or type declaration is implicitly a `static` member. A `static` member can't be referenced through an instance. Instead, it's referenced through the type name. For example, consider the following class:
To refer to the static member `x`, use the fully qualified name, `MyBaseC.MyStruct.x`, unless the member is accessible from the same scope:
25
+
To refer to the `static` member `x`, use the fully qualified name, `MyBaseC.MyStruct.x`, unless the member is accessible from the same scope:
28
26
29
27
```csharp
30
28
Console.WriteLine(MyBaseC.MyStruct.x);
31
29
```
32
30
33
-
While an instance of a class contains a separate copy of all instance fields of the class, there is only one copy of each static field.
31
+
While an instance of a class contains a separate copy of all instance fields of the class, there's only one copy of each `static` field.
34
32
35
-
It is not possible to use [this](this.md) to reference static methods or property accessors.
33
+
It isn't possible to use [`this`](this.md) to reference `static` methods or property accessors.
36
34
37
-
If the `static` keyword is applied to a class, all the members of the class must be static.
35
+
If the `static` keyword is applied to a class, all the members of the class must be `static`.
38
36
39
-
Classesand static classes may have static constructors. Static constructors are called at some point between when the program starts and the class is instantiated.
37
+
Classes, interfaces, and `static` classes may have `static` constructors. A `static` constructor is called at some point between when the program starts and the class is instantiated.
40
38
41
39
> [!NOTE]
42
40
> The `static` keyword has more limited uses than in C++. To compare with the C++ keyword, see [Storage classes (C++)](/cpp/cpp/storage-classes-cpp#static).
43
41
44
-
To demonstrate static members, consider a class that represents a company employee. Assume that the class contains a method to count employees and a field to store the number of employees. Both the method and the field do not belong to any instance employee. Instead they belong to the company class. Therefore, they should be declared as static members of the class.
42
+
To demonstrate `static` members, consider a class that represents a company employee. Assume that the class contains a method to count employees and a field to store the number of employees. Both the method and the field don't belong to any one employee instance. Instead, they belong to the class of employees as a whole. They should be declared as `static` members of the class.
45
43
46
44
## Example
47
45
48
-
This example reads the name and ID of a new employee, increments the employee counter by one, and displays the information for the new employee and the new number of employees. For simplicity, this program reads the current number of employees from the keyboard. In a real application, this information should be read from a file.
46
+
This example reads the name and ID of a new employee, increments the employee counter by one, and displays the information for the new employee and the new number of employees. This program reads the current number of employees from the keyboard.
This example shows that although you can initialize a static field by using another static field not yet declared, the results will be undefined until you explicitly assign a value to the static field.
52
+
This example shows that you can initialize a `static` field by using another `static` field that is not yet declared. The results will be undefined until you explicitly assign a value to the `static` field.
0 commit comments