June 29, 2011

Contravariance and Covariance


Good simple MSDN guide
Many definitions here, choose the one that is easiest to understand
Here is the Wikipedia view


Covariant: converting from a broader type (more generic) to a narrower (more specific) type. Can use a more derived type than that specified in the definition in an output (returned type). Covariance means that you can use an object of a specialized class (e.g. String) where the declaration states a more general class (e.g. object).

Contravariant: converting from a narrower (more specific) type to a broader (more generic) type.  Can use a base type of that specified in the definition. Contravariance means that you can use an object of a more general class (e.g. object) where the declaration states a more specific class (e.g. string).

No comments: