开发者

interface covariance issue

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.

interface Animal
{
    Animal GetAnimal();
}

class Cat : Animal
{
   //Not ALlowed
   Cat GetAnimal()
   {
       return this;
   }

   //Allowed
   Animal GetAnimal()
   {
       return this;
   }   
}

The problem is that C as a specialization of B<C> returns C I<C>.GetT(), however the specification of J requires J GetT().

Try the following:

interface I<out T>
    where T : class, I<T>
{
    T GetT();
}

interface J : I<J>
{
}

abstract class B<T,U> : I<U>
    where T : B<T,U>, U
    where U : class, I<U>
{
    U I<U>.GetT()
    {
        return null;
    }
}

class C : B<C,J>, J
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜