KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
First of, I have read many explanations on SO and blogs about covariance and contravariance and a big thanks goes out to Eric Lippert for producing such a great series on Covariance and Contravariance . However I have a more specific question that I am trying to get my head around a little bit. As far as I understand per Eric's explanation is that Covariance and Contravariance are both adjectives that describe a transformation. Covariant transformation is that which preserves the order of types and Contravariant transformation is one that reverses it. I understand covariance in such a manner that I think most developers understand intuitively. //covariant operation Animal someAnimal = new Giraffe(); //assume returns Mammal, also covariant operation someAnimal = Mammal.GetSomeMammal(); The return operation here is covariant as we are preserving the size in which both Animal is still bigger than Mammal or Giraffe. On that note most return operations are covariant, contravariant operations would not make sense. //if return operations were contravariant //the following would be illegal //as Mammal would need to be stored in something //equal to or less derived than Mammal //which would mean that Animal is now less than or equal than Mammal //therefore reversing the relationship Animal someAnimal = Mammal.GetSomeMammal(); This piece of code of course would not make sense to most developers. My confusion lies in Contravariant argument parameters. If you had a method such as bool Compare(Mammal mammal1, Mammal mammal2);
Tags (comma-separated)
Save Edits
Cancel