The following code sample:
interface I<out T>
where T : class, I<T>
{
T GetT();
}
interface J : I<J>
{
}
abstract class B<T> : I<T>
where T : B<T>
{
T I<T>.GetT()
{
return null;
}
}
class C : B<C>, J
{
}
fails to compile (under VS2010 with SP1) with the following error:
Error 4 'C' does not implement interface member 'I<J>.GetT()'
However, C does implement (through its base B<C>) I<C>, which, due to I being declared covariant, should capture I<J> as well (as C : J).
Is this a compiler bug? If not, why am I not allowed to do that?
Even though it is covariant, you cannot change the return type of the interface. This is no different from the covariance in non-Generic classes.
The problem is that C as a specialization of
B<C>returnsC I<C>.GetT(), however the specification of J requiresJ GetT().Try the following: