18 Interfaces
18.1 General
An interface defines a contract. A class or struct that implements an interface shall adhere to its contract. An interface may inherit from multiple base interfaces, and a class or struct may implement multiple interfaces.
Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide implementations for the members that it declares. The interface merely specifies the members that shall be supplied by classes or structs that implement the interface.
18.2 Interface declarations
18.2.1 General
An interface_declaration is a type_declaration (§14.7) that declares a new interface type.
interface_declaration
: attributes? interface_modifier* 'partial'? 'interface'
identifier variant_type_parameter_list? interface_base?
type_parameter_constraints_clause* interface_body ';'?
;
An interface_declaration consists of an optional set of attributes (§22), followed by an optional set of interface_modifiers (§18.2.2), followed by an optional partial modifier (§15.2.7), followed by the keyword interface
and an identifier that names the interface, followed by an optional variant_type_parameter_list specification (§18.2.3), followed by an optional interface_base specification (§18.2.4), followed by an optional type_parameter_constraints_clauses specification (§15.2.5), followed by an interface_body (§18.3), optionally followed by a semicolon.
An interface declaration shall not supply a type_parameter_constraints_clauses unless it also supplies a variant_type_parameter_list.
An interface declaration that supplies a variant_type_parameter_list is a generic interface declaration. Additionally, any interface nested inside a generic class declaration or a generic struct declaration is itself a generic interface declaration, since type arguments for the containing type shall be supplied to create a constructed type (§8.4).
18.2.2 Interface modifiers
An interface_declaration may optionally include a sequence of interface modifiers:
interface_modifier
: 'new'
| 'public'
| 'protected'
| 'internal'
| 'private'
| unsafe_modifier // unsafe code support
;
unsafe_modifier (§23.2) is only available in unsafe code (§23).
It is a compile-time error for the same modifier to appear multiple times in an interface declaration.
The new
modifier is only permitted on interfaces defined within a class. It specifies that the interface hides an inherited member by the same name, as described in §15.3.5.
The public
, protected
, internal
, and private
modifiers control the accessibility of the interface. Depending on the context in which the interface declaration occurs, only some of these modifiers might be permitted (§7.5.2). When a partial type declaration (§15.2.7) includes an accessibility specification (via the public
, protected
, internal
, and private
modifiers), the rules in §15.2.2 apply.
18.2.3 Variant type parameter lists
18.2.3.1 General
Variant type parameter lists can only occur on interface and delegate types. The difference from ordinary type_parameter_lists is the optional variance_annotation on each type parameter.
variant_type_parameter_list
: '<' variant_type_parameters '>'
;
variant_type_parameters
: attributes? variance_annotation? type_parameter
| variant_type_parameters ',' attributes? variance_annotation?
type_parameter
;
variance_annotation
: 'in'
| 'out'
;
If the variance annotation is out
, the type parameter is said to be covariant. If the variance annotation is in
, the type parameter is said to be contravariant. If there is no variance annotation, the type parameter is said to be invariant.
Example: In the following:
interface C<out X, in Y, Z> { X M(Y y); Z P { get; set; } }
X
is covariant,Y
is contravariant andZ
is invariant.end example
If a generic interface is declared in multiple parts (§15.2.3), each partial declaration shall specify the same variance for each type parameter.
18.2.3.2 Variance safety
The occurrence of variance annotations in the type parameter list of a type restricts the places where types can occur within the type declaration.
A type T is output-unsafe if one of the following holds:
T
is a contravariant type parameterT
is an array type with an output-unsafe element typeT
is an interface or delegate typeSᵢ,... Aₑ
constructed from a generic typeS<Xᵢ, ... Xₑ>
where for at least oneAᵢ
one of the following holds:Xᵢ
is covariant or invariant andAᵢ
is output-unsafe.Xᵢ
is contravariant or invariant andAᵢ
is input-unsafe.
A type T is input-unsafe if one of the following holds:
T
is a covariant type parameterT
is an array type with an input-unsafe element typeT
is an interface or delegate typeS<Aᵢ,... Aₑ>
constructed from a generic typeS<Xᵢ, ... Xₑ>
where for at least oneAᵢ
one of the following holds:Xᵢ
is covariant or invariant andAᵢ
is input-unsafe.Xᵢ
is contravariant or invariant andAᵢ
is output-unsafe.
Intuitively, an output-unsafe type is prohibited in an output position, and an input-unsafe type is prohibited in an input position.
A type is output-safe if it is not output-unsafe, and input-safe if it is not input-unsafe.
18.2.3.3 Variance conversion
The purpose of variance annotations is to provide for more lenient (but still type safe) conversions to interface and delegate types. To this end the definitions of implicit (§10.2) and explicit conversions (§10.3) make use of the notion of variance-convertibility, which is defined as follows:
A type T<Aᵢ, ..., Aᵥ>
is variance-convertible to a type T<Bᵢ, ..., Bᵥ>
if T
is either an interface or a delegate type declared with the variant type parameters T<Xᵢ, ..., Xᵥ>
, and for each variant type parameter Xᵢ
one of the following holds:
Xᵢ
is covariant and an implicit reference or identity conversion exists fromAᵢ
toBᵢ
Xᵢ
is contravariant and an implicit reference or identity conversion exists fromBᵢ
toAᵢ
Xᵢ
is invariant and an identity conversion exists fromAᵢ
toBᵢ
18.2.4 Base interfaces
An interface can inherit from zero or more interface types, which are called the explicit base interfaces of the interface. When an interface has one or more explicit base interfaces, then in the declaration of that interface, the interface identifier is followed by a colon and a comma-separated list of base interface types.
interface_base
: ':' interface_type_list
;
The explicit base interfaces can be constructed interface types (§8.4, §18.2). A base interface cannot be a type parameter on its own, though it can involve the type parameters that are in scope.
For a constructed interface type, the explicit base interfaces are formed by taking the explicit base interface declarations on the generic type declaration, and substituting, for each type_parameter in the base interface declaration, the corresponding type_argument of the constructed type.
The explicit base interfaces of an interface shall be at least as accessible as the interface itself (§7.5.5).
Note: For example, it is a compile-time error to specify a
private
orinternal
interface in the interface_base of apublic
interface. end note
It is a compile-time error for an interface to directly or indirectly inherit from itself.
The base interfaces of an interface are the explicit base interfaces and their base interfaces. In other words, the set of base interfaces is the complete transitive closure of the explicit base interfaces, their explicit base interfaces, and so on. An interface inherits all members of its base interfaces.
Example: In the following code
interface IControl { void Paint(); } interface ITextBox : IControl { void SetText(string text); } interface IListBox : IControl { void SetItems(string[] items); } interface IComboBox: ITextBox, IListBox {}
the base interfaces of
IComboBox
areIControl
,ITextBox
, andIListBox
. In other words, theIComboBox
interface above inherits membersSetText
andSetItems
as well asPaint
.end example
Members inherited from a constructed generic type are inherited after type substitution. That is, any constituent types in the member have the base class declaration’s type parameters replaced with the corresponding type arguments used in the class_base specification.
Example: In the following code
interface IBase<T> { T[] Combine(T a, T b); } interface IDerived : IBase<string[,]> { // Inherited: string[][,] Combine(string[,] a, string[,] b); }
the interface
IDerived
inherits theCombine
method after the type parameterT
is replaced withstring[,]
.end example
A class or struct that implements an interface also implicitly implements all of the interface’s base interfaces.
The handling of interfaces on multiple parts of a partial interface declaration (§15.2.7) are discussed further in §15.2.4.3.
Every base interface of an interface shall be output-safe (§18.2.3.2).
18.3 Interface body
The interface_body of an interface defines the members of the interface.
interface_body
: '{' interface_member_declaration* '}'
;
18.4 Interface members
18.4.1 General
The members of an interface are the members inherited from the base interfaces and the members declared by the interface itself.
interface_member_declaration
: interface_method_declaration
| interface_property_declaration
| interface_event_declaration
| interface_indexer_declaration
;
An interface declaration declares zero or more members. The members of an interface shall be methods, properties, events, or indexers. An interface cannot contain constants, fields, operators, instance constructors, finalizers, or types, nor can an interface contain static members of any kind.
All interface members implicitly have public access. It is a compile-time error for interface member declarations to include any modifiers.
An interface_declaration creates a new declaration space (§7.3), and the type parameters and interface_member_declarations immediately contained by the interface_declaration introduce new members into this declaration space. The following rules apply to interface_member_declarations:
- The name of a type parameter in the variant_type_parameter_list of an interface declaration shall differ from the names of all other type parameters in the same variant_type_parameter_list and shall differ from the names of all members of the interface.
- The name of a method shall differ from the names of all properties and events declared in the same interface. In addition, the signature (§7.6) of a method shall differ from the signatures of all other methods declared in the same interface, and two methods declared in the same interface shall not have signatures that differ solely by
in
,out
, andref
. - The name of a property or event shall differ from the names of all other members declared in the same interface.
- The signature of an indexer shall differ from the signatures of all other indexers declared in the same interface.
The inherited members of an interface are specifically not part of the declaration space of the interface. Thus, an interface is allowed to declare a member with the same name or signature as an inherited member. When this occurs, the derived interface member is said to hide the base interface member. Hiding an inherited member is not considered an error, but it does cause a compiler to issue a warning. To suppress the warning, the declaration of the derived interface member shall include a new
modifier to indicate that the derived member is intended to hide the base member. This topic is discussed further in §7.7.2.3.
If a new
modifier is included in a declaration that doesn’t hide an inherited member, a warning is issued to that effect. This warning is suppressed by removing the new
modifier.
Note: The members in class
object
are not, strictly speaking, members of any interface (§18.4). However, the members in classobject
are available via member lookup in any interface type (§12.5). end note
The set of members of an interface declared in multiple parts (§15.2.7) is the union of the members declared in each part. The bodies of all parts of the interface declaration share the same declaration space (§7.3), and the scope of each member (§7.7) extends to the bodies of all the parts.
18.4.2 Interface methods
Interface methods are declared using interface_method_declarations:
interface_method_declaration
: attributes? 'new'? return_type interface_method_header
| attributes? 'new'? ref_kind ref_return_type interface_method_header
;
interface_method_header
: identifier '(' parameter_list? ')' ';'
| identifier type_parameter_list '(' parameter_list? ')'
type_parameter_constraints_clause* ';'
;
The attributes, return_type, ref_return_type, identifier, and parameter_list of an interface method declaration have the same meaning as those of a method declaration in a class (§15.6). An interface method declaration is not permitted to specify a method body, and the declaration therefore always ends with a semicolon.
All parameter types of an interface method shall be input-safe (§18.2.3.2), and the return type shall be either void
or output-safe. In addition, any output or reference parameter types shall also be output-safe.
Note: Output parameters are required to be input-safe due to common implementation restrictions. end note
Furthermore, each class type constraint, interface type constraint and type parameter constraint on any type parameters of the method shall be input-safe.
Furthermore, each class type constraint, interface type constraint and type parameter constraint on any type parameter of the method shall be input-safe.
These rules ensure that any covariant or contravariant usage of the interface remains typesafe.
Example:
interface I<out T> { void M<U>() where U : T; // Error }
is ill-formed because the usage of
T
as a type parameter constraint onU
is not input-safe.Were this restriction not in place it would be possible to violate type safety in the following manner:
class B {} class D : B {} class E : B {} class C : I<D> { public void M<U>() {...} } ... I<B> b = new C(); b.M<E>();
This is actually a call to
C.M<E>
. But that call requires thatE
derive fromD
, so type safety would be violated here.end example
18.4.3 Interface properties
Interface properties are declared using interface_property_declarations:
interface_property_declaration
: attributes? 'new'? type identifier '{' interface_accessors '}'
| attributes? 'new'? ref_kind type identifier '{' ref_interface_accessor '}'
;
interface_accessors
: attributes? 'get' ';'
| attributes? 'set' ';'
| attributes? 'get' ';' attributes? 'set' ';'
| attributes? 'set' ';' attributes? 'get' ';'
;
ref_interface_accessor
: attributes? 'get' ';'
;
The attributes, type, and identifier of an interface property declaration have the same meaning as those of a property declaration in a class (§15.7).
The accessors of an interface property declaration correspond to the accessors of a class property declaration (§15.7.3), except that the accessor_body shall always be a semicolon. Thus, the accessors simply indicate whether the property is read-write, read-only, or write-only.
The type of an interface property shall be output-safe if there is a get accessor, and shall be input-safe if there is a set accessor.
18.4.4 Interface events
Interface events are declared using interface_event_declarations:
interface_event_declaration
: attributes? 'new'? 'event' type identifier ';'
;
The attributes, type, and identifier of an interface event declaration have the same meaning as those of an event declaration in a class (§15.8).
The type of an interface event shall be input-safe.
18.4.5 Interface indexers
Interface indexers are declared using interface_indexer_declarations:
interface_indexer_declaration
: attributes? 'new'? type 'this' '[' parameter_list ']'
'{' interface_accessors '}'
| attributes? 'new'? ref_kind type 'this' '[' parameter_list ']'
'{' ref_interface_accessor '}'
;
The attributes, type, and parameter_list of an interface indexer declaration have the same meaning as those of an indexer declaration in a class (§15.9).
The accessors of an interface indexer declaration correspond to the accessors of a class indexer declaration (§15.9), except that the accessor_body shall always be a semicolon. Thus, the accessors simply indicate whether the indexer is read-write, read-only, or write-only.
All the parameter types of an interface indexer shall be input-safe (§18.2.3.2). In addition, any output or reference parameter types shall also be output-safe.
Note: Output parameters are required to be input-safe due to common implementation restrictions. end note
The type of an interface indexer shall be output-safe if there is a get accessor, and shall be input-safe if there is a set accessor.
18.4.6 Interface member access
Interface members are accessed through member access (§12.8.7) and indexer access (§12.8.12.3) expressions of the form I.M
and I[A]
, where I
is an interface type, M
is a method, property, or event of that interface type, and A
is an indexer argument list.
For interfaces that are strictly single-inheritance (each interface in the inheritance chain has exactly zero or one direct base interface), the effects of the member lookup (§12.5), method invocation (§12.8.10.2), and indexer access (§12.8.12.3) rules are exactly the same as for classes and structs: More derived members hide less derived members with the same name or signature. However, for multiple-inheritance interfaces, ambiguities can occur when two or more unrelated base interfaces declare members with the same name or signature. This subclause shows several examples, some of which lead to ambiguities and others which don’t. In all cases, explicit casts can be used to resolve the ambiguities.
Example: In the following code
interface IList { int Count { get; set; } } interface ICounter { void Count(int i); } interface IListCounter : IList, ICounter {} class C { void Test(IListCounter x) { x.Count(1); // Error x.Count = 1; // Error ((IList)x).Count = 1; // Ok, invokes IList.Count.set ((ICounter)x).Count(1); // Ok, invokes ICounter.Count } }
the first two statements cause compile-time errors because the member lookup (§12.5) of
Count
inIListCounter
is ambiguous. As illustrated by the example, the ambiguity is resolved by castingx
to the appropriate base interface type. Such casts have no run-time costs—they merely consist of viewing the instance as a less derived type at compile-time.end example
Example: In the following code
interface IInteger { void Add(int i); } interface IDouble { void Add(double d); } interface INumber : IInteger, IDouble {} class C { void Test(INumber n) { n.Add(1); // Invokes IInteger.Add n.Add(1.0); // Only IDouble.Add is applicable ((IInteger)n).Add(1); // Only IInteger.Add is a candidate ((IDouble)n).Add(1); // Only IDouble.Add is a candidate } }
the invocation
n.Add(1)
selectsIInteger.Add
by applying overload resolution rules of §12.6.4. Similarly, the invocationn.Add(1.0)
selectsIDouble.Add
. When explicit casts are inserted, there is only one candidate method, and thus no ambiguity.end example
Example: In the following code
interface IBase { void F(int i); } interface ILeft : IBase { new void F(int i); } interface IRight : IBase { void G(); } interface IDerived : ILeft, IRight {} class A { void Test(IDerived d) { d.F(1); // Invokes ILeft.F ((IBase)d).F(1); // Invokes IBase.F ((ILeft)d).F(1); // Invokes ILeft.F ((IRight)d).F(1); // Invokes IBase.F } }
the
IBase.F
member is hidden by theILeft.F
member. The invocationd.F(1)
thus selectsILeft.F
, even thoughIBase.F
appears to not be hidden in the access path that leads throughIRight
.The intuitive rule for hiding in multiple-inheritance interfaces is simply this: If a member is hidden in any access path, it is hidden in all access paths. Because the access path from
IDerived
toILeft
toIBase
hidesIBase.F
, the member is also hidden in the access path fromIDerived
toIRight
toIBase
.end example
18.5 Qualified interface member names
An interface member is sometimes referred to by its qualified interface member name. The qualified name of an interface member consists of the name of the interface in which the member is declared, followed by a dot, followed by the name of the member. The qualified name of a member references the interface in which the member is declared.
Example: Given the declarations
interface IControl { void Paint(); } interface ITextBox : IControl { void SetText(string text); }
the qualified name of
Paint
isIControl.Paint
and the qualified name of SetText isITextBox.SetText
. In the example above, it is not possible to refer toPaint
asITextBox.Paint
.end example
When an interface is part of a namespace, a qualified interface member name can include the namespace name.
Example:
namespace System { public interface ICloneable { object Clone(); } }
Within the
System
namespace, bothICloneable.Clone
andSystem.ICloneable.Clone
are qualified interface member names for theClone
method.end example
18.6 Interface implementations
18.6.1 General
Interfaces may be implemented by classes and structs. To indicate that a class or struct directly implements an interface, the interface is included in the base class list of the class or struct.
Example:
interface ICloneable { object Clone(); } interface IComparable { int CompareTo(object other); } class ListEntry : ICloneable, IComparable { public object Clone() {...} public int CompareTo(object other) {...} }
end example
A class or struct that directly implements an interface also implicitly implements all of the interface’s base interfaces. This is true even if the class or struct doesn’t explicitly list all base interfaces in the base class list.
Example:
interface IControl { void Paint(); } interface ITextBox : IControl { void SetText(string text); } class TextBox : ITextBox { public void Paint() {...} public void SetText(string text) {...} }
Here, class
TextBox
implements bothIControl
andITextBox
.end example
When a class C
directly implements an interface, all classes derived from C
also implement the interface implicitly.
The base interfaces specified in a class declaration can be constructed interface types (§8.4, §18.2).
Example: The following code illustrates how a class can implement constructed interface types:
class C<U, V> {} interface I1<V> {} class D : C<string, int>, I1<string> {} class E<T> : C<int, T>, I1<T> {}
end example
The base interfaces of a generic class declaration shall satisfy the uniqueness rule described in §18.6.3.
18.6.2 Explicit interface member implementations
For purposes of implementing interfaces, a class or struct may declare explicit interface member implementations. An explicit interface member implementation is a method, property, event, or indexer declaration that references a qualified interface member name.
Example:
interface IList<T> { T[] GetElements(); } interface IDictionary<K, V> { V this[K key] { get; } void Add(K key, V value); } class List<T> : IList<T>, IDictionary<int, T> { public T[] GetElements() {...} T IDictionary<int, T>.this[int index] {...} void IDictionary<int, T>.Add(int index, T value) {...} }
Here
IDictionary<int,T>.this
andIDictionary<int,T>.Add
are explicit interface member implementations.end example
Example: In some cases, the name of an interface member might not be appropriate for the implementing class, in which case, the interface member may be implemented using explicit interface member implementation. A class implementing a file abstraction, for example, would likely implement a
Close
member function that has the effect of releasing the file resource, and implement theDispose
method of theIDisposable
interface using explicit interface member implementation:interface IDisposable { void Dispose(); } class MyFile : IDisposable { void IDisposable.Dispose() => Close(); public void Close() { // Do what's necessary to close the file System.GC.SuppressFinalize(this); } }
end example
It is not possible to access an explicit interface member implementation through its qualified interface member name in a method invocation, property access, event access, or indexer access. An explicit interface member implementation can only be accessed through an interface instance, and is in that case referenced simply by its member name.
It is a compile-time error for an explicit interface member implementation to include any modifiers (§15.6) other than extern
or async
.
It is a compile-time error for an explicit interface method implementation to include type_parameter_constraints_clauses. The constraints for a generic explicit interface method implementation are inherited from the interface method.
Note: Explicit interface member 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. Explicit interface member implementations serve two primary purposes:
- Because explicit interface member implementations are not accessible through class or struct instances, they allow interface implementations to be excluded from the public interface of a class or struct. This is particularly useful when a class or struct implements an internal interface that is of no interest to a consumer of that class or struct.
- Explicit interface member implementations allow disambiguation of interface members with the same signature. Without explicit interface member implementations it would be impossible for a class or struct to have different implementations of interface members with the same signature and return type, as would it be impossible for a class or struct to have any implementation at all of interface members with the same signature but with different return types.
end note
For an explicit interface member implementation to be valid, the class or struct shall name an interface in its base class list that contains a member whose qualified interface member name, type, number of type parameters, and parameter types exactly match those of the explicit interface member implementation. If an interface function member has a parameter array, the corresponding parameter of an associated explicit interface member implementation is allowed, but not required, to have the params
modifier. If the interface function member does not have a parameter array then an associated explicit interface member implementation shall not have a parameter array.
Example: Thus, in the following class
class Shape : ICloneable { object ICloneable.Clone() {...} int IComparable.CompareTo(object other) {...} // invalid }
the declaration of
IComparable.CompareTo
results in a compile-time error becauseIComparable
is not listed in the base class list ofShape
and is not a base interface ofICloneable
. Likewise, in the declarationsclass Shape : ICloneable { object ICloneable.Clone() {...} } class Ellipse : Shape { object ICloneable.Clone() {...} // invalid }
the declaration of
ICloneable.Clone
inEllipse
results in a compile-time error becauseICloneable
is not explicitly listed in the base class list ofEllipse
.end example
The qualified interface member name of an explicit interface member implementation shall reference the interface in which the member was declared.
Example: Thus, in the declarations
interface IControl { void Paint(); } interface ITextBox : IControl { void SetText(string text); } class TextBox : ITextBox { void IControl.Paint() {...} void ITextBox.SetText(string text) {...} }
the explicit interface member implementation of Paint must be written as
IControl.Paint
, notITextBox.Paint
.end example
18.6.3 Uniqueness of implemented interfaces
The interfaces implemented by a generic type declaration shall remain unique for all possible constructed types. Without this rule, it would be impossible to determine the correct method to call for certain constructed types.
Example: Suppose a generic class declaration were permitted to be written as follows:
interface I<T> { void F(); } class X<U ,V> : I<U>, I<V> // Error: I<U> and I<V> conflict { void I<U>.F() {...} void I<V>.F() {...} }
Were this permitted, it would be impossible to determine which code to execute in the following case:
I<int> x = new X<int, int>(); x.F();
end example
To determine if the interface list of a generic type declaration is valid, the following steps are performed:
- Let
L
be the list of interfaces directly specified in a generic class, struct, or interface declarationC
. - Add to
L
any base interfaces of the interfaces already inL
. - Remove any duplicates from
L
. - If any possible constructed type created from
C
would, after type arguments are substituted intoL
, cause two interfaces inL
to be identical, then the declaration ofC
is invalid. Constraint declarations are not considered when determining all possible constructed types.
Note: In the class declaration
X
above, the interface listL
consists ofl<U>
andI<V>
. The declaration is invalid because any constructed type withU
andV
being the same type would cause these two interfaces to be identical types. end note
It is possible for interfaces specified at different inheritance levels to unify:
interface I<T>
{
void F();
}
class Base<U> : I<U>
{
void I<U>.F() {...}
}
class Derived<U, V> : Base<U>, I<V> // Ok
{
void I<V>.F() {...}
}
This code is valid even though Derived<U,V>
implements both I<U>
and I<V>
. The code
I<int> x = new Derived<int, int>();
x.F();
invokes the method in Derived
, since Derived<int,int>'
effectively re-implements I<int>
(§18.6.7).
18.6.4 Implementation of generic methods
When a generic method implicitly implements an interface method, the constraints given for each method type parameter shall be equivalent in both declarations (after any interface type parameters are replaced with the appropriate type arguments), where method type parameters are identified by ordinal positions, left to right.
Example: In the following code:
interface I<X, Y, Z> { void F<T>(T t) where T : X; void G<T>(T t) where T : Y; void H<T>(T t) where T : Z; } class C : I<object, C, string> { public void F<T>(T t) {...} // Ok public void G<T>(T t) where T : C {...} // Ok public void H<T>(T t) where T : string {...} // Error }
the method
C.F<T>
implicitly implementsI<object,C,string>.F<T>
. In this case,C.F<T>
is not required (nor permitted) to specify the constraintT: object
sinceobject
is an implicit constraint on all type parameters. The methodC.G<T>
implicitly implementsI<object,C,string>.G<T>
because the constraints match those in the interface, after the interface type parameters are replaced with the corresponding type arguments. The constraint for methodC.H<T>
is an error because sealed types (string
in this case) cannot be used as constraints. Omitting the constraint would also be an error since constraints of implicit interface method implementations are required to match. Thus, it is impossible to implicitly implementI<object,C,string>.H<T>
. This interface method can only be implemented using an explicit interface member implementation:class C : I<object, C, string> { ... public void H<U>(U u) where U : class {...} void I<object, C, string>.H<T>(T t) { string s = t; // Ok H<T>(t); } }
In this case, the explicit interface member implementation invokes a public method having strictly weaker constraints. The assignment from t to s is valid since
T
inherits a constraint ofT: string
, even though this constraint is not expressible in source code. end example
Note: When a generic method explicitly implements an interface method no constraints are allowed on the implementing method (§15.7.1, §18.6.2). end note
18.6.5 Interface mapping
A class or struct shall provide implementations of all members of the interfaces that are listed in the base class list of the class or struct. The process of locating implementations of interface members in an implementing class or struct is known as interface mapping.
Interface mapping for a class or struct C
locates an implementation for each member of each interface specified in the base class list of C
. The implementation of a particular interface member I.M
, where I
is the interface in which the member M
is declared, is determined by examining each class or struct S
, starting with C
and repeating for each successive base class of C
, until a match is located:
- If
S
contains a declaration of an explicit interface member implementation that matchesI
andM
, then this member is the implementation ofI.M
. - Otherwise, if
S
contains a declaration of a non-static public member that matchesM
, then this member is the implementation ofI.M
. If more than one member matches, it is unspecified which member is the implementation ofI.M
. This situation can only occur ifS
is a constructed type where the two members as declared in the generic type have different signatures, but the type arguments make their signatures identical.
A compile-time error occurs if implementations cannot be located for all members of all interfaces specified in the base class list of C
. The members of an interface include those members that are inherited from base interfaces.
Members of a constructed interface type are considered to have any type parameters replaced with the corresponding type arguments as specified in §15.3.3.
Example: For example, given the generic interface declaration:
interface I<T> { T F(int x, T[,] y); T this[int y] { get; } }
the constructed interface
I<string[]>
has the members:string[] F(int x, string[,][] y); string[] this[int y] { get; }
end example
For purposes of interface mapping, a class or struct member A
matches an interface member B
when:
A
andB
are methods, and the name, type, and parameter lists ofA
andB
are identical.A
andB
are properties, the name and type ofA
andB
are identical, andA
has the same accessors asB
(A
is permitted to have additional accessors if it is not an explicit interface member implementation).A
andB
are events, and the name and type ofA
andB
are identical.A
andB
are indexers, the type and parameter lists ofA
andB
are identical, andA
has the same accessors asB
(A
is permitted to have additional accessors if it is not an explicit interface member implementation).
Notable implications of the interface-mapping algorithm are:
- Explicit interface member implementations take precedence over other members in the same class or struct when determining the class or struct member that implements an interface member.
- Neither non-public nor static members participate in interface mapping.
Example: In the following code
interface ICloneable { object Clone(); } class C : ICloneable { object ICloneable.Clone() {...} public object Clone() {...} }
the
ICloneable.Clone
member ofC
becomes the implementation ofClone
in ‘ICloneable’ because explicit interface member implementations take precedence over other members.end example
If a class or struct implements two or more interfaces containing a member with the same name, type, and parameter types, it is possible to map each of those interface members onto a single class or struct member.
Example:
interface IControl { void Paint(); } interface IForm { void Paint(); } class Page : IControl, IForm { public void Paint() {...} }
Here, the
Paint
methods of bothIControl
andIForm
are mapped onto thePaint
method inPage
. It is of course also possible to have separate explicit interface member implementations for the two methods.end example
If a class or struct implements an interface that contains hidden members, then some members may need to be implemented through explicit interface member implementations.
Example:
interface IBase { int P { get; } } interface IDerived : IBase { new int P(); }
An implementation of this interface would require at least one explicit interface member implementation, and would take one of the following forms
class C1 : IDerived { int IBase.P { get; } int IDerived.P() {...} } class C2 : IDerived { public int P { get; } int IDerived.P() {...} } class C3 : IDerived { int IBase.P { get; } public int P() {...} }
end example
When a class implements multiple interfaces that have the same base interface, there can be only one implementation of the base interface.
Example: In the following code
interface IControl { void Paint(); } interface ITextBox : IControl { void SetText(string text); } interface IListBox : IControl { void SetItems(string[] items); } class ComboBox : IControl, ITextBox, IListBox { void IControl.Paint() {...} void ITextBox.SetText(string text) {...} void IListBox.SetItems(string[] items) {...} }
it is not possible to have separate implementations for the
IControl
named in the base class list, theIControl
inherited byITextBox
, and theIControl
inherited byIListBox
. Indeed, there is no notion of a separate identity for these interfaces. Rather, the implementations ofITextBox
andIListBox
share the same implementation ofIControl
, andComboBox
is simply considered to implement three interfaces,IControl
,ITextBox
, andIListBox
.end example
The members of a base class participate in interface mapping.
Example: In the following code
interface Interface1 { void F(); } class Class1 { public void F() {} public void G() {} } class Class2 : Class1, Interface1 { public new void G() {} }
the method
F
inClass1
is used inClass2's
implementation ofInterface1
.end example
18.6.6 Interface implementation inheritance
A class inherits all interface implementations provided by its base classes.
Without explicitly re-implementing an interface, a derived class cannot in any way alter the interface mappings it inherits from its base classes.
Example: In the declarations
interface IControl { void Paint(); } class Control : IControl { public void Paint() {...} } class TextBox : Control { public new void Paint() {...} }
the
Paint
method inTextBox
hides thePaint
method inControl
, but it does not alter the mapping ofControl.Paint
ontoIControl.Paint
, and calls toPaint
through class instances and interface instances will have the following effectsControl c = new Control(); TextBox t = new TextBox(); IControl ic = c; IControl it = t; c.Paint(); // invokes Control.Paint(); t.Paint(); // invokes TextBox.Paint(); ic.Paint(); // invokes Control.Paint(); it.Paint(); // invokes Control.Paint();
end example
However, when an interface method is mapped onto a virtual method in a class, it is possible for derived classes to override the virtual method and alter the implementation of the interface.
Example: Rewriting the declarations above to
interface IControl { void Paint(); } class Control : IControl { public virtual void Paint() {...} } class TextBox : Control { public override void Paint() {...} }
the following effects will now be observed
Control c = new Control(); TextBox t = new TextBox(); IControl ic = c; IControl it = t; c.Paint(); // invokes Control.Paint(); t.Paint(); // invokes TextBox.Paint(); ic.Paint(); // invokes Control.Paint(); it.Paint(); // invokes TextBox.Paint();
end example
Since explicit interface member implementations cannot be declared virtual, it is not possible to override an explicit interface member implementation. However, it is perfectly valid for an explicit interface member implementation to call another method, and that other method can be declared virtual to allow derived classes to override it.
Example:
interface IControl { void Paint(); } class Control : IControl { void IControl.Paint() { PaintControl(); } protected virtual void PaintControl() {...} } class TextBox : Control { protected override void PaintControl() {...} }
Here, classes derived from
Control
can specialize the implementation ofIControl.Paint
by overriding thePaintControl
method.end example
18.6.7 Interface re-implementation
A class that inherits an interface implementation is permitted to re-implement the interface by including it in the base class list.
A re-implementation of an interface follows exactly the same interface mapping rules as an initial implementation of an interface. Thus, the inherited interface mapping has no effect whatsoever on the interface mapping established for the re-implementation of the interface.
Example: In the declarations
interface IControl { void Paint(); } class Control : IControl { void IControl.Paint() {...} } class MyControl : Control, IControl { public void Paint() {} }
the fact that
Control
mapsIControl.Paint
ontoControl.IControl.Paint
doesn’t affect the re-implementation inMyControl
, which mapsIControl.Paint
ontoMyControl.Paint
.end example
Inherited public member declarations and inherited explicit interface member declarations participate in the interface mapping process for re-implemented interfaces.
Example:
interface IMethods { void F(); void G(); void H(); void I(); } class Base : IMethods { void IMethods.F() {} void IMethods.G() {} public void H() {} public void I() {} } class Derived : Base, IMethods { public void F() {} void IMethods.H() {} }
Here, the implementation of
IMethods
inDerived
maps the interface methods ontoDerived.F
,Base.IMethods.G
,Derived.IMethods.H
, andBase.I
.end example
When a class implements an interface, it implicitly also implements all that interface’s base interfaces. Likewise, a re-implementation of an interface is also implicitly a re-implementation of all of the interface’s base interfaces.
Example:
interface IBase { void F(); } interface IDerived : IBase { void G(); } class C : IDerived { void IBase.F() {...} void IDerived.G() {...} } class D : C, IDerived { public void F() {...} public void G() {...} }
Here, the re-implementation of
IDerived
also re-implementsIBase
, mappingIBase.F
ontoD.F
.end example
18.6.8 Abstract classes and interfaces
Like a non-abstract class, an abstract class shall provide implementations of all members of the interfaces that are listed in the base class list of the class. However, an abstract class is permitted to map interface methods onto abstract methods.
Example:
interface IMethods { void F(); void G(); } abstract class C : IMethods { public abstract void F(); public abstract void G(); }
Here, the implementation of
IMethods
mapsF
andG
onto abstract methods, which shall be overridden in non-abstract classes that derive fromC
.end example
Explicit interface member implementations cannot be abstract, but explicit interface member implementations are of course permitted to call abstract methods.
Example:
interface IMethods { void F(); void G(); } abstract class C: IMethods { void IMethods.F() { FF(); } void IMethods.G() { GG(); } protected abstract void FF(); protected abstract void GG(); }
Here, non-abstract classes that derive from
C
would be required to overrideFF
andGG
, thus providing the actual implementation ofIMethods
.end example
ECMA C# draft specification