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
Copy file name to clipboardExpand all lines: docs/csharp/fundamentals/types/interfaces.md
+15-1Lines changed: 15 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ For more information about abstract classes, see [Abstract and Sealed Classes an
26
26
27
27
Interfaces can contain instance methods, properties, events, indexers, or any combination of those four member types. Interfaces may contain static constructors, fields, constants, or operators. Beginning with C# 11, interface members that aren't fields may be `static abstract`. An interface can't contain instance fields, instance constructors, or finalizers. Interface members are public by default, and you can explicitly specify accessibility modifiers, such as `public`, `protected`, `internal`, `private`, `protected internal`, or `private protected`. A `private` member must have a default implementation.
28
28
29
-
To implement an interface member, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member.
29
+
To implement an interface member using implicit implementation, the corresponding member of the implementing class must be public, non-static, and have the same name and signature as the interface member. However, when an interface is meant to be internal only or uses internal types in its signature, you can use explicit interface implementation instead, which doesn't require the implementing member to be public.
30
30
31
31
> [!NOTE]
32
32
> When an interface declares static members, a type implementing that interface may also declare static members with the same signature. Those are distinct and uniquely identified by the type declaring the member. The static member declared in a type *doesn't* override the static member declared in the interface.
@@ -43,6 +43,20 @@ Interfaces can inherit from one or more interfaces. The derived interface inheri
43
43
44
44
A base class can also implement interface members by using virtual members. In that case, a derived class can change the interface behavior by overriding the virtual members. For more information about virtual members, see [Polymorphism](../object-oriented/polymorphism.md).
45
45
46
+
## Working with internal interfaces
47
+
48
+
An internal interface can typically be implemented using implicit implementation with public members, as long as all the types in the interface signature are publicly accessible. However, when an interface uses internal types in its member signatures, implicit implementation becomes impossible because the implementing class member would need to be public while exposing internal types. In such cases, you must use explicit interface implementation.
In the preceding example, the `IConfigurable` interface uses an internal type `InternalConfiguration` in its method signature. The `ServiceImplementation` class cannot use implicit implementation because that would require making the `Configure` method public, which isn't allowed when the method signature contains internal types. Instead, explicit interface implementation is used, which doesn't have an access modifier and is only accessible through the interface type.
55
+
56
+
In contrast, the `ILoggable` interface can be implemented implicitly with public members because all types in its signature (`string`) are publicly accessible, even though the interface itself is internal.
57
+
58
+
For more information about explicit interface implementation, see [Explicit Interface Implementation](../../programming-guide/interfaces/explicit-interface-implementation.md).
0 commit comments