I've come across these in a textbook I am reading on C#, but I am having difficulty understanding them, probably due to lack of context.
Is there a good concise explanation of what they are and what they are useful for out there?
Edit for clarification:
Covariant interface:
interface IBibble<out T>
.
.
Contravariant interface:
interface IBibble<in T>
.
.
This post is the best I've read on the subject
In short, covariance / contravariance /invariance deals with automatic type casting (from base to derived and vice-versa). Those type casts are possible only if some guarantees are respected in terms of read / write actions performed on the casted objects. Read the post for more details.
With
<out T>
, you can treat the interface reference as one upwards in the hierarchy.With
<in T>
, you can treat the interface reference as one downwards in the hiearchy.Let me try to explain it in more english terms.
Let's say you are retrieving a list of animals from your zoo, and you intend to process them. All animals (in your zoo) have a name, and a unique ID. Some animals are mammals, some are reptiles, some are amphibians, some are fish, etc. but they're all animals.
So, with your list of animals (which contains animals of different types), you can say that all the animals have a name, so obviously it would be safe to get the name of all the animals.
However, what if you have a list of fishes only, but need to treat them like animals, does that work? Intuitively, it should work, but in C# 3.0 and before, this piece of code will not compile:
The reason for this is that the compiler doesn't "know" what you intend, or can, do with the animals collection after you've retrieved it. For all it knows, there could be a way through
IEnumerable<T>
to put an object back into the list, and that would potentially allow you to put an animal that isn't a fish, into a collection that is supposed to contain only fish.In other words, the compiler cannot guarantee that this is not allowed:
So the compiler just outright refuses to compile your code. This is covariance.
Let's look at contravariance.
Since our zoo can handle all animals, it can certainly handle fish, so let's try to add some fish to our zoo.
In C# 3.0 and before, this does not compile:
Here, the compiler could allow this piece of code, even though the method returns
List<Animal>
simply because all fishes are animals, so if we just changed the types to this:Then it would work, but the compiler cannot determine that you're not trying to do this:
Since the list is actually a list of animals, this is not allowed.
So contra- and co-variance is how you treat object references and what you're allowed to do with them.
The
in
andout
keywords in C# 4.0 specifically marks the interface as one or the other. Within
, you're allowed to place the generic type (usually T) in input-positions, which means method arguments, and write-only properties.With
out
, you're allowed to place the generic type in output-positions, which is method return values, read-only properties, and out method parameters.This will allow you to do what intended to do with the code:
List<T>
has both in- and out-directions on T, so it is neither co-variant nor contra-variant, but an interface that allowed you to add objects, like this:would allow you to do this:
Here's a few videos that shows the concepts:
Here's an example:
Without these marks, the following could compile:
or this: