What is Covariance, Contravariance and Invariance?

Asked

Viewed 31 times

1

Reading this article by Microsoft I came across these three terms, with the following meanings (translation via Google Translate):

  • Covariance

Allows to use a type more derived than originally specified. You can assign an instance of IEnumerable<Derived> to a variable of the type IEnumerable<Base>.

  • Contravariance

Allows you to use a more generic type (less derivative) than originally specified. You can assign an instance of IEnumerable<Base> to a variable of the type IEnumerable<Derived>.

  • Invariance

It means that you can only use the originally specified type; so a generic invariant type parameter is neither covariant nor contravariant.

Unable to assign an instance of IEnumerable<Base> to a type variable IEnumerable<Derived> or vice versa.

I would like to know, in other words, what those words mean?

  • That? http://answall.com/q/32880/101 Practical example: http://answall.com/q/75097/101

  • @bigown I searched and did not find. I will vote to close here, thank you very much :p

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.