Skip to content

Commit 73bcc6d

Browse files
BillWagnerYoussef1313gewarren
authored
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]>
1 parent 166ca2f commit 73bcc6d

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

docs/csharp/language-reference/compiler-messages/cs0106.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ The modifier 'modifier' is not valid for this item
1313

1414
A class or interface member was marked with an invalid access modifier. The following examples describe some of these invalid modifiers:
1515

16-
- The [static](../keywords/static.md) and [public](../keywords/public.md) modifiers are not permitted on interface methods.
17-
1816
- The [static](../keywords/static.md) modifier is not permitted on a [local function](../../programming-guide/classes-and-structs/local-functions.md).
1917

2018
- The `public` keyword is not allowed on an explicit interface declaration. In this case, remove the `public` keyword from the explicit interface declaration.
@@ -38,7 +36,6 @@ namespace MyNamespace
3836
interface I
3937
{
4038
void m();
41-
static public void f(); // CS0106
4239
}
4340

4441
public class MyClass

docs/csharp/language-reference/keywords/interface.md

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,57 @@
11
---
22
title: "interface - C# Reference"
3-
ms.date: 07/20/2015
3+
ms.date: 01/17/2020
44
f1_keywords:
55
- "interface_CSharpKeyword"
66
helpviewer_keywords:
77
- "interface keyword [C#]"
88
ms.assetid: 7da38e81-4f99-4bc5-b07d-c986b687eeba
99
---
10-
# interface (C# Reference)
10+
# :::no-loc text="interface"::: (C# Reference)
1111

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`.
1315

1416
For more information and examples, see [Interfaces](../../programming-guide/interfaces/index.md).
1517

1618
## Example
1719

1820
[!code-csharp[csrefKeywordsTypes#14](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsTypes/CS/keywordsTypes.cs#14)]
1921

20-
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:
2123

2224
- [Methods](../../programming-guide/classes-and-structs/methods.md)
23-
2425
- [Properties](../../programming-guide/classes-and-structs/using-properties.md)
25-
2626
- [Indexers](../../programming-guide/indexers/using-indexers.md)
27-
2827
- [Events](event.md)
2928

30-
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:
30+
31+
- [Constants](const.md)
32+
- [Operators](../operators/operator-overloading.md)
33+
- [Static constructor](../../programming-guide/classes-and-structs/constructors.md#static-constructors).
34+
- [Nested types](../../programming-guide/classes-and-structs/nested-types.md)
35+
- [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+
public interface INamed
43+
{
44+
public string Name {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.
3149

3250
When a base type list contains a base class and interfaces, the base class must come first in the list.
3351

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.
3553

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).
3755

3856
## Example
3957

@@ -43,7 +61,7 @@ The following example demonstrates interface implementation. In this example, th
4361

4462
## C# language specification
4563

46-
[!INCLUDE[CSharplangspec](~/includes/csharplangspec-md.md)]
64+
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)
4765

4866
## See also
4967

docs/csharp/language-reference/keywords/static.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "static modifier - C# Reference"
3-
ms.date: 07/20/2015
3+
ms.date: 01/22/2020
44
f1_keywords:
55
- "static"
66
- "static_CSharpKeyword"
@@ -10,48 +10,46 @@ ms.assetid: 5509e215-2183-4da3-bab4-6b7e607a4fdf
1010
---
1111
# static (C# Reference)
1212

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 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 indexers or finalizers. For more information, see [Static Classes and Static Class Members](../../programming-guide/classes-and-structs/static-classes-and-static-class-members.md).
1414

1515
## Example
1616

1717
The following class is declared as `static` and contains only `static` methods:
1818

1919
[!code-csharp[csrefKeywordsModifiers#18](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#18)]
2020

21-
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:
2422

2523
[!code-csharp[csrefKeywordsModifiers#19](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#19)]
2624

27-
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:
2826

2927
```csharp
3028
Console.WriteLine(MyBaseC.MyStruct.x);
3129
```
3230

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.
3432

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.
3634

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`.
3836

39-
Classes and 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.
4038

4139
> [!NOTE]
4240
> 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).
4341
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.
4543

4644
## Example
4745

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.
4947

5048
[!code-csharp[csrefKeywordsModifiers#20](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#20)]
5149

5250
## Example
5351

54-
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.
5553

5654
[!code-csharp[csrefKeywordsModifiers#21](~/samples/snippets/csharp/VS_Snippets_VBCSharp/csrefKeywordsModifiers/CS/csrefKeywordsModifiers.cs#21)]
5755

0 commit comments

Comments
 (0)