What makes ValueTuple covariant?

I believe what is actually happening here is a destructuring assignment. Tuple assignment will try to implicitly convert its components, and as it is possible to assign string to object, that is what happens here. The language supports assignment between tuple types that have the same number of elements, where each right-hand side element can … Read more

C# variance annotation of a type parameter, constrained to be value type

Why this is allowed by compiler since variance annotation make completely no sense in a such situation? It’s allowed by the compiler because I never even considered that someone might try to do that when I added the variance rules to the C# 4.0 compiler. Compiler warnings and errors are features, and in order for … Read more

Contravariance explained

Update: Ooops. As it turned out, I mixed up variance and “assignment compatibility” in my initial answer. Edited the answer accordingly. Also I wrote a blog post that I hope should answer such questions better: Covariance and Contravariance FAQ Answer: I guess the answer to your first question is that you don’t have contravariance in … Read more

Simple examples of co and contravariance

Could someone provide me simple C# examples of convariance, contravariance, invariance and contra-invariance (if such thing exists). I have no idea what “contra-invariance” means. The rest are easy. Here’s an example of covariance: void FeedTheAnimals(IEnumerable<Animal> animals) { foreach(Animal animal in animals) animal.Feed(); } … List<Giraffe> giraffes = …; FeedTheAnimals(giraffes); The IEnumerable<T> interface is covariant. The … Read more

Generic type parameter covariance and multiple interface implementations

If you have tested both of: class DoubleDown: IGeneric<Derived1>, IGeneric<Derived2> { string IGeneric<Derived1>.GetName() { return “Derived1”; } string IGeneric<Derived2>.GetName() { return “Derived2”; } } class DoubleDown: IGeneric<Derived2>, IGeneric<Derived1> { string IGeneric<Derived1>.GetName() { return “Derived1”; } string IGeneric<Derived2>.GetName() { return “Derived2”; } } You must have realized that the results in reality, changes with the order … Read more

C# : Is Variance (Covariance / Contravariance) another word for Polymorphism?

It’s certainly related to polymorphism. I wouldn’t say they’re just “another word” for polymorphism though – they’re about very specific situations, where you can treat one type as if it were another type in a certain context. For instance, with normal polymorphism you can treat any reference to a Banana as a reference to a … Read more

Python typing what does TypeVar(A, B, covariant=True) mean?

Covariance and contra-variance are terms that relate to the intersection between object orientation and generics. Here’s the question this concept is trying to answer: We have a couple of “regular”, “object-oriented” classes, Base and Derived. We also have some generic type – let’s say List[T]. We know that Derived can be used anywhere Base can … Read more