Interface covariance issue?

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.GetT() however the specification of J requires J GetT() Try the following: interface I where T : class, I { T GetT(); } interface J : I { } abstract class B : I where T : B, U where U : class, I { U I.GetT() { return null; } } class C : B, J { }.

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 returns C I.GetT(), however the specification of J requires J GetT().

Try the following: interface I where T : class, I { T GetT(); } interface J : I { } abstract class B : I where T : B, U where U : class, I { U I.GetT() { return null; } } class C : B, J { }.

It seems then that variance is only useful "from the outside" - here I is only convertible to I, an implementation of the former is not an implementation of the latter. Am I correct? – Grzegorz Herman Jun 8 at 12:02 @Grzegorz, you are asking another question.

The reason the code doesn't compile has nothing to do with covariance, it has to do with the fact that CLR languages do not allow you to modify the signature of an interface method in an implementation - even if it is a constraint. Personally, I find the covariant feature for generics to be mostly useful when dealing with Generic collections. I.e.

IList bList = new IList(){}; (where C : B). – Ethan Cabiac Jun 8 at 12:35 @Ethan: This compiles correctly: interface I { T GetT(); } class A { } class B : A { } class C : I { B I.GetT() { return null; } static void M() { I ia = new C(); } }. Here, the implementation of I serves (automatically) as an implementation of I, even though the return types of GetT are different.It is not clear (at least to me) why it should not work in the original case.

And, by the way, IIRC IList is not covariant... – Grzegorz Herman Jun 8 at 13:27 Moreover, if I add I next to I, the code fails to compile - even though the compiler was perfectly happy to treat an object of class C as I until I explicitly said it is one! – Grzegorz Herman Jun 8 at 13:33 It does not seem to be a compiler issue - mono-2.10.2 behaves exactly like VS on all of the above examples. – Grzegorz Herman Jun 8 at 13:36.

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 returns C I.GetT(), however the specification of J requires J GetT().

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions