Can't get co/contra-variance to work in C?

To use generic variance in C# you have to meet all the following conditions: Use C# 4 The type that varies must be a generic interface or generic delegate The type parameter must be marked "in" (contravariant) or "out" (covariant) The type parameter annotations must yield a type which is provably typesafe in all its possible operations The "source" type argument and the "destination" type argument must have an identity or reference conversion between them Your program meets condition 1 and condition 5 but not conditions 2, 3, or 4 There is no way to get the variance you want because you want something that would violate condition 4 Watch what happens when we meet all the conditions except condition 4: Suppose this declaration were legal: interface IMyCollection { List Items { get; set; } } class Animal {} class AnimalCollection : IMyCollection { public List { get; set; } } class Giraffe : Animal {} class GiraffeCollection : IMyCollection { public List { get; set; } } static class X { public IMyCollection GetThing() { // IMyCollection is covariant in T, so this is legal. Return new GiraffeCollection(); } } class Tiger : Animal {} ... IMyCollection animals = X.GetThing(); // GetThing() actually returns a GiraffeCollection animals. Items = new List() { new Tiger(); } And a giraffe collection now contains a list of animals that contains a tiger You have to be typesafe if you're going to use variance Because the compiler cannot determine the variance annotations to be typesafe, it rejects the declaration of IMyCollection Let's see how we might do this and be typesafe The problem is that Items is writable via the interface interface IMyCollection { IEnumerable Items { get; } } class Animal {} class AnimalCollection : IMyCollection { public IEnumerable { get { yield return new Tiger(); } } } class Giraffe : Animal {} class GiraffeCollection : IMyCollection { public IEnumerable { get { yield return new Giraffe(); } } } static class X { public IMyCollection GetThing() { return new GiraffeCollection(); } } class Tiger : Animal {} ... MyCollection animals = X.GetThing(); // GetThing() actually returns a GiraffeCollection foreach(Animal animal in animals.

Items) { ... } // Items yields a giraffe, which is an animal Perfectly typesafe. This would be a legal C# 4 program If the design details of covariance and contravariance in C# 4 interests you, you might consider reading my dozen articles on the subject. You can find them here: http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance Note that these are listed in most-to-least-recent order; start from the bottom If in particular you are interested in the rules for determining when an interface's annotations are valid, see this article (which I just discovered and fixed some errors in, so I'm glad we had this conversation.) http://blogs.msdn.com/b/ericlippert/archive/2009/12/03/exact-rules-for-variance-validity.aspx.

To use generic variance in C# you have to meet all the following conditions: Use C# 4 The type that varies must be a generic interface or generic delegate The type parameter must be marked "in" (contravariant) or "out" (covariant) The type parameter annotations must yield a type which is provably typesafe in all its possible operations The "source" type argument and the "destination" type argument must have an identity or reference conversion between them. Your program meets condition 1 and condition 5 but not conditions 2, 3, or 4. There is no way to get the variance you want because you want something that would violate condition 4.

Watch what happens when we meet all the conditions except condition 4: // Suppose this declaration were legal: interface IMyCollection { List Items { get; set; } } class Animal {} class AnimalCollection : IMyCollection { public List { get; set; } } class Giraffe : Animal {} class GiraffeCollection : IMyCollection { public List { get; set; } } static class X { public IMyCollection GetThing() { // IMyCollection is covariant in T, so this is legal. Return new GiraffeCollection(); } } class Tiger : Animal {} ... IMyCollection animals = X.GetThing(); // GetThing() actually returns a GiraffeCollection animals. Items = new List() { new Tiger(); } And a giraffe collection now contains a list of animals that contains a tiger.

You have to be typesafe if you're going to use variance. Because the compiler cannot determine the variance annotations to be typesafe, it rejects the declaration of IMyCollection. Let's see how we might do this and be typesafe.

The problem is that Items is writable via the interface. Interface IMyCollection { IEnumerable Items { get; } } class Animal {} class AnimalCollection : IMyCollection { public IEnumerable { get { yield return new Tiger(); } } } class Giraffe : Animal {} class GiraffeCollection : IMyCollection { public IEnumerable { get { yield return new Giraffe(); } } } static class X { public IMyCollection GetThing() { return new GiraffeCollection(); } } class Tiger : Animal {} ... MyCollection animals = X.GetThing(); // GetThing() actually returns a GiraffeCollection foreach(Animal animal in animals. Items) { ... } // Items yields a giraffe, which is an animal Perfectly typesafe.

This would be a legal C# 4 program. If the design details of covariance and contravariance in C# 4 interests you, you might consider reading my dozen articles on the subject. You can find them here: http://blogs.msdn.com/b/ericlippert/archive/tags/covariance+and+contravariance/ Note that these are listed in most-to-least-recent order; start from the bottom.

If in particular you are interested in the rules for determining when an interface's annotations are valid, see this article (which I just discovered and fixed some errors in, so I'm glad we had this conversation. ) http://blogs.msdn.com/b/ericlippert/archive/2009/12/03/exact-rules-for-variance-validity.aspx.

1 for being so thorough with the explanation. – sholsinger Apr 7 at 14:28.

There is no relation between E and C. So your return type must be A which may be considered as a common parent to those types. Also A needs to be an interface where the generic argument is defined with out for this to work: interface A { } class B { } class C : A { } class D : B { } class E : A { } static class X { public static A GetThing(bool f) { if (f) { return new E(); } return new C(); } } Couldn't make it work with the ternary operator (?:).

The compiler simply doesn't like it. UPDATE: After @Eric Lippert's excellent remark in the comments section you could use the ternary operator by casting to A: public static A GetThing(bool f) { return f? (A)new E() : new C(); }.

Actually I did have A as the return type, that was a typo, so I'll update the question. Incidentally, my A abstract class contains a property virtual List { get; set; } which causes a problem too... – enashnash Apr 7 at 11:53 Sorry, that should have been List Items { get; set; } – enashnash Apr 7 at 12:06 1 If you cast one or both sides of the : to A then the conditional operator should work. The problem is that the conditional operator tries to find a type that is the type of the expression and that type must be the type of at least one of the consequence or alternative.In C# we never "magic up" a type that doesn't appear in the expression.

We don't say "foo? Giraffe : cat", oh, neither Giraffe nor Cat are convertible to each other, let's pick Mammal. Mammal isn't an option because it doesn't appear.

– Eric Lippert Apr 7 at 14:05 @Eric Lippert, thanks for this valuable precision. I've updated my answer to take it into account. @enashnash, because T is covariant in the generic A interface definition you can only have the generic argument as input argument.

You cannot have it appear as a method result so this cannot work in your case because you have a property returning List. – Darin Dimitrov Apr 7 at 14:10.

This cannot work because C and E have no common parent. To something like this you need to create a common parent type, be it a class or an interface.

The "source" type argument and the "destination" type argument must have an identity or reference conversion between them. Your program meets condition 1 and condition 5 but not conditions 2, 3, or 4. There is no way to get the variance you want because you want something that would violate condition 4.

And a giraffe collection now contains a list of animals that contains a tiger. You have to be typesafe if you're going to use variance. Because the compiler cannot determine the variance annotations to be typesafe, it rejects the declaration of IMyCollection.

Let's see how we might do this and be typesafe. The problem is that Items is writable via the interface. This would be a legal C# 4 program.

If the design details of covariance and contravariance in C# 4 interests you, you might consider reading my dozen articles on the subject. Note that these are listed in most-to-least-recent order; start from the bottom.

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