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

Casting List – covariance/contravariance problem

The simplest way is probably to use ConvertAll: List<IMyClass> converted = original.ConvertAll<IMyClass>(x => x); Even if you’re using .NET 2, you can use lambda syntax if you’re using VS2008 or higher. Otherwise, there’s always anonymous methods: List<IMyClass> converted = original.ConvertAll<IMyClass>( delegate (MyClass x) { return x; }); In .NET 3.5 you could use LINQ with … Read more

Generics : List

List<Dog> is a subtype of List<? extends Animal>, but not a subtype of List<Animal>. Why is List<Dog> not a subtype of List<Animal>? Consider the following example: void mySub(List<Animal> myList) { myList.add(new Cat()); } If you were allowed to pass a List<Dog> to this function, you would get a run-time error. EDIT: Now, if we use … Read more

Difference between Variance, Covariance, Contravariance and Bivariance in TypeScript

Variance has to do with how a generic type F<T> varies with respect to its type parameter T. If you know that T extends U, then variance will tell you whether you can conclude that F<T> extends F<U>, conclude that F<U> extends F<T>, or neither, or both. Covariance means that F<T> and T co–vary. That … Read more

Give examples of functions which demonstrate covariance and contravariance in the cases of both overloading and overriding in Java? [closed]

Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because … Read more

Difference between Variance, Covaraince, Contravariance and Bivariance in TypeScript

Variance has to do with how a generic type F<T> varies with respect to its type parameter T. If you know that T extends U, then variance will tell you whether you can conclude that F<T> extends F<U>, conclude that F<U> extends F<T>, or neither, or both. Covariance means that F<T> and T co–vary. That … Read more

How do I return a reference to something inside a RefCell without breaking encapsulation?

You can create a new struct similar to the Ref<‘a,T> guard returned by RefCell::borrow(), in order to wrap this Ref and avoid having it going out of scope, like this: use std::cell::Ref; struct FooGuard<‘a> { guard: Ref<‘a, MutableInterior>, } then, you can implement the Deref trait for it, so that it can be used as … Read more