Difference between Covariance & Contra-variance

The question is “what is the difference between covariance and contravariance?” Covariance and contravariance are properties of a mapping function that associates one member of a set with another. More specifically, a mapping can be covariant or contravariant with respect to a relation on that set. Consider the following two subsets of the set of … Read more

Covariance and contravariance real world example

// Contravariance interface IGobbler<in T> { void gobble(T t); } // Since a QuadrupedGobbler can gobble any four-footed // creature, it is OK to treat it as a donkey gobbler. IGobbler<Donkey> dg = new QuadrupedGobbler(); dg.gobble(MyDonkey()); // Covariance interface ISpewer<out T> { T spew(); } // A MouseSpewer obviously spews rodents (all mice are // … Read more

Why are arrays covariant but generics are invariant?

Via wikipedia: Early versions of Java and C# did not include generics (a.k.a. parametric polymorphism). In such a setting, making arrays invariant rules out useful polymorphic programs. For example, consider writing a function to shuffle an array, or a function that tests two arrays for equality using the Object.equals method on the elements. The implementation … Read more

vs in Generics

The out keyword in generics is used to denote that the type T in the interface is covariant. See Covariance and contravariance for details. The classic example is IEnumerable<out T>. Since IEnumerable<out T> is covariant, you’re allowed to do the following: IEnumerable<string> strings = new List<string>(); IEnumerable<object> objects = strings; The second line above would … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)