Adsence750x90

Tuesday, June 14, 2016

Covariance, Contravariance and Invariance



Covariance and contravariance are terms that refer to the ability to use a less derived (less specific) or more derived type (more specific) than originally specified. Generic type parameters support covariance and contravariance to provide greater flexibility in assigning and using generic types. When you are referring to a type system, covariance, contravariance, and invariance have the following definitions. The examples assume a base class named Base and a derived class named Derived



Covariance

Enables you to use a more derived type than originally specified.
You can assign an instance of IEnumerable<Derived> (IEnumerable(Of Derived) in Visual Basic) to a variable of type IEnumerable<Base>.

Contravariance

Enables you to use a more generic (less derived) type than originally specified.
You can assign an instance of IEnumerable<Base> (IEnumerable(Of Base) in Visual Basic) to a variable of type IEnumerable<Derived>.

Invariance

Means that you can use only the type originally specified; so an invariant generic type parameter is neither covariant nor contravariant.
You cannot assign an instance of IEnumerable<Base> (IEnumerable(Of Base) in Visual Basic) to a variable of type IEnumerable<Derived> or vice versa.


Read more

Covariance and Contravariance in Generics

What is covariance and contravariance in c# ?

1 comment:

Anonymous said...

Hi there just wanted to give you a quick heads up.
The words in your content seem to be running off the screen in Opera.
I'm not sure if this is a format issue or something to do with internet browser compatibility but I thought I'd post to let you know.
The design and style look great though! Hope you get the issue fixed
soon. Many thanks