How does Java’s use-site variance compare to C#’s declaration site variance?

I am just going to answer the differences between declaration-site and use-site variance, since, while C# and Java generics differ in many other ways, those differences are mostly orthogonal to variance. First off, if I remember correctly use-site variance is strictly more powerful than declaration-site variance (although at the cost of concision), or at least … Read more

ref and out parameters in C# and cannot be marked as variant

“out” means, roughly speaking, “only appears in output positions”. “in” means, roughly speaking, “only appears in input positions”. The real story is a bit more complicated than that, but the keywords were chosen because most of the time this is the case. Consider a method of an interface or the method represented by a delegate: … 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

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

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