Information Technology Reference
In-Depth Information
Generic Interface Implementations Must Be Unique
When implementing an interface in a generic type, there must be no possible combination
of type arguments that would create a duplicate interface in the type.
For example, in the following code, class
Trivial
uses two instantiations of interface
IMyIfc
.
The first one is a constructed type, instantiated with type
int
.
The second one has a type parameter rather than an argument.
This causes a conflict because a class must implement all its declared interfaces. But if
int
is used as the type argument to replace
S
in the second interface, then
Trivial
would have two
interfaces of the same type—which is not allowed.
interface IMyIfc<T>
{
T ReturnIt(T inValue);
}
Two interfaces
↓
↓
class Trivial<S> : IMyIfc<int>, IMyIfc<S> // Error!
{
public int ReturnIt(int inValue) // Implement first interface
{
return inValue;
}
public S ReturnIt(S inValue) // Implement second interface,
{ // but if it's int, it would be
return inValue; // the same as the one above.
}
}