It's certainly related to polymorphism. I wouldn't say they're just "another word" for polymorphism though - they're about very specific situations, where you can treat one type as if it were another type in a certain context For instance, with normal polymorphism you can treat any reference to a Banana as a reference to a Fruit but that doesn't mean you can substitute Fruit every time you see the type Banana For example, a ListAs a return value). Contravariance allows a "smaller" (more specific) type to be substituted in an API where the original type is only used in an "input" position It's hard to go into all the details in a single SO post (although hopefully someone else will do a better job than this!).
Eric Lippert has an excellent series of blog posts about it.
It's certainly related to polymorphism. I wouldn't say they're just "another word" for polymorphism though - they're about very specific situations, where you can treat one type as if it were another type in a certain context. For instance, with normal polymorphism you can treat any reference to a Banana as a reference to a Fruit - but that doesn't mean you can substitute Fruit every time you see the type Banana.
For example, a List can't be treated as a List because list. Add(new Apple()) is valid for List but not for List. Covariance allows a "bigger" (less specific) type to be substituted in an API where the original type is only used in an "output" position (e.g.As a return value).
Contravariance allows a "smaller" (more specific) type to be substituted in an API where the original type is only used in an "input" position. It's hard to go into all the details in a single SO post (although hopefully someone else will do a better job than this!). Eric Lippert has an excellent series of blog posts about it.
Ah yes infact I was reading Lippert's posts, and found them quite helpful – Andreas Grech Jul 3 '09 at 9:07 The blog posts were all published in October 2007, I think, so they are available on this page: blogs.msdn. Com/ericlippert/archive/2007/10. Aspx – Fredrik Mörk Jul 3 '09 at 9:08 Nope, there was another one in the series very recently ("The void is invariant") – Jon Skeet Jul 3 '09 at 9:09 Eric's posts on this are awesome.
Might I suggest adding in to the answer a list of said links since his blog software is not working on the tags. – ShuggyCoUk Jul 3 '09 at 9:14 1 @ShuggyCoUk: I edited Jon's answer so the link works now. – Fredrik Mörk Jul 3 '09 at 9:16.
Thanks for all the shout-outs, guys. Jon and Rasmus's answers are fine, I would just add a quick technical note. When speaking casually and informally, yes, people use "covariance" and "contravariance" to refer to a specific kind of polymorphism.
That is, the kind of polymorphism where you treat a sequence of spiders as though it were a sequence of animals. Were we to get all computer-sciency and try to make more technical definitions, then I probably would not say that covariance and contravariance are "a kind of polymorphism". I would approach a more technical definition like this: First, I'd note that there are two possible kinds of polymorphism in C# that you might be talking about, and it is important to not confuse them.
The first kind is traditionally called "ad hoc polymorphism", and that's the polymorphism where you have a method M(Animal x), and you pass spiders and giraffes and wallabies to it, and the method uniformly treats its passed-in arguments the same way by using the commonalities guaranteed by the Animal base class. The second kind is traditionally called "parametric polymorphism", or "generic polymorphism". That's the ability to make a generic method M(T t) and then have a bunch of code in the method that again, treats the argument uniformly based on commonalities guaranteed by the constraints on T.
I think you're talking about the first kind of polymorphism. But my point is just that we can define polymorphism as the ability of a programming language to treat different things uniformly based on a known commonality. (For example, a known base type, or known implemented interface.) Covariance and contravariance is the ability of a programming language to take advantage of commonalities between generic types deduced from known commonalities of their type arguments.
You can think about co- and contravariance as being an advanced form of polymorphism. Not only can you use a child-class as if it was its parent-class, with co- and contravariance, the polymorphism extends to classes that relates to the polymorphic classes. Imagine two classes: public class Pet { /*...*/ } public class Cat:Pet { /*...*/ } Polymorphism is being able to use a Cat as a Pet: void Feed(Pet pet) { /* ... */ } Cat cat = ... Feed(cat); Co- and contravariance is used to talk about being able to use an ICollection as an ICollection (covariance): void FeedAll(ICollection pets) { /* ... */ } List cats = ... FeedAll(cats); or to use an Action as an Action (contravariance): Action GetFeeder() { /* ... */ } Action feeder = GetFeeder(); Eric Lippert wrote a great blog series about it when they were first designing the feature.
Part one is here.
– Ray Vega May 11 '10 at 21:09 @Ray Thanks. Fixed. – Rasmus Faber May 12 '10 at 7:07.
Covariance and Contravariance in C#, Part Eight: Syntax Options Covariance and Contravariance in C#, Part Nine: Breaking Changes Covariance and Contravariance in C#, Part Ten: Dealing With Ambiguity Covariance and Contravariance, Part Eleven: To infinity, but not beyond.
Yup, that's Lippert's collection – Andreas Grech Nov 6 '09 at 7:10.
I think is is special kind of polymorphism not another word for it. It is polymorphism in delegates where a delegate with a return type of base can accept child type.
1 no it's aspects of polymorphism in function parameters and return values. This means it affects delegates but it doesn't require them – ShuggyCoUk Jul 3 '09 at 9:13.
When speaking casually and informally, yes, people use "covariance" and "contravariance" to refer to a specific kind of polymorphism. That is, the kind of polymorphism where you treat a sequence of spiders as though it were a sequence of animals. Were we to get all computer-sciency and try to make more technical definitions, then I probably would not say that covariance and contravariance are "a kind of polymorphism".
First, I'd note that there are two possible kinds of polymorphism in C# that you might be talking about, and it is important to not confuse them. The first kind is traditionally called "ad hoc polymorphism", and that's the polymorphism where you have a method M(Animal x), and you pass spiders and giraffes and wallabies to it, and the method uniformly treats its passed-in arguments the same way by using the commonalities guaranteed by the Animal base class. The second kind is traditionally called "parametric polymorphism", or "generic polymorphism".
That's the ability to make a generic method M(T t) and then have a bunch of code in the method that again, treats the argument uniformly based on commonalities guaranteed by the constraints on T. I think you're talking about the first kind of polymorphism. But my point is just that we can define polymorphism as the ability of a programming language to treat different things uniformly based on a known commonality.
Covariance and contravariance is the ability of a programming language to take advantage of commonalities between generic types deduced from known commonalities of their type arguments.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.