Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0aa0133

Browse files
RexJaeschkejskeet
authored andcommitted
Add support for constraints on explicit interface implementations
1 parent0ec14db commit0aa0133

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

‎standard/interfaces.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,26 @@ It is not possible to access an explicit interface member implementation through
659659
660660
It is a compile-time error for an explicit interface member implementation to include any modifiers ([§15.6](classes.md#156-methods)) other than `extern` or `async`.
661661
662-
It is a compile-time error for an explicit interface method implementation to include *type_parameter_constraints_clause*s. The constraints for a generic explicit interface method implementation are inherited from the interface method.
662+
If *type_parameter_constraints_clause*s are present in an explicit interface method implementation, each of their *type_parameter_constraints* shall be `class` or `struct`, and for the constraint `class` in the implementation shall correspond to a type parameter in the interface method that is known to be a non-nullable reference type. Any type parameter that has the `struct` constraint in the implementation shall correspond to a type parameter in the interface method that is known to be a non-nullable value type. In the absence of *type_parameter_constraints_clause*s, the constraints for an explicit interface method implementation are inherited from the interface method, and for the constraint `class` a parameter type `T?` is interpreted as `System.Nullable<T>`.
663+
664+
> *Example*:Thefollowingdemonstrateshowtherulesworkwhentypeparametersareinvolved:
665+
>
666+
> ```csharp
667+
> #nullableenable
668+
>interfaceI
669+
> {
670+
>voidFoo<T>(T?value)whereT :class;
671+
>voidFoo<T>(T?value)whereT :struct;
672+
> }
673+
>
674+
>classC :I
675+
> {
676+
> void I.Foo<T>(T? value) where T : class { }
677+
> void I.Foo<T>(T? value) where T : struct { }
678+
> }
679+
> ```
680+
>
681+
>Withoutthetypeparametersintheimplementingmethods,thecompilerwontknowwhichmethodisbeingimplemented. *endexample*
663682
664683
> *Note*:Explicitinterfacemember implementations have different accessibility characteristics than other members. Because explicit interface member implementations are never accessible through a qualified interface member name in a method invocation or a property access, they are in a sense private. However, since they can be accessed through the interface, they are in a sense also as public as the interface in which they are declared.
665684
> Explicit interface member implementations serve two primary purposes:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp