Covariance and Contravariance FAQ

What are covariance and contravariance?
In C#, covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments.Covariance preserves assignment compatibility and contravariance reverses it.
The following code demonstrates the difference between assignment compatibility, covariance, and contravariance.

In C#, variance is supported in the following scenarios:
Covariance in arrays (since C# 1.0)
Covariance and contravariance in delegates, also known as “method group variance” (since C# 2.0)
Variance for generic type parameters in interfaces and delegates (since C# 4.0)
What is array covariance?
Arrays are covariant since C# 1.0. You can always do the following:

In the above code, I assigned an array of strings to an array of objects. So I used a more derived type than that originally specified, which is covariance.
Covariance in arrays is considered “not safe,” because you can also do this:

This code compiles, but it throws an exception at run time because obj is, in fact, an array of strings and cannot contain integers.
What is delegate, or method group, variance?
This feature was added in C# 2.0. When you instantiate a delegate, you can assign it a method that has a more derived return type than that specified in the delegate (covariance). You can also assign a method that has parameter types less derived than those in the delegate (contravariance).
Here’s a quick code example illustrating the feature and some of its limitations.

By the way, this feature works for all delegates, both generic and non-generic, not just for Func and Action delegates.
For more information and examples, see Covariance and Contravariance in Delegates on MSDN and Eric Lippert’s post Covariance and Contravariance in C#, Part Three: Method Group Conversion Variance.
What is variance for generic type parameters?
This is a new feature in C# 4.0. Now, when creating a generic interface, you can specify whether there is an implicit conversion between interface instances that have different type arguments. For example, you can use an interface instance that has methods with more derived return types than originally specified (covariance) or that has methods with less derived parameter types (contravariance). The same rules are applied to generic delegates.
While you can create variant interfaces and delegates yourself, this is not the main purpose for this feature. What is more important is that a set of interfaces and delegates in .NET Framework 4 have been updated to become variant.
Here’s the list of updated interfaces:

And the list of updated delegates:
Action delegates from the System namespace, for example, Action<T> and Action<T1, T2> (T, T1, T2, and so on are contravariant)
Func delegates from the System namespace, for example, Func<TResult> and Func<t, tresult=””> (TResult is covariant; T, T1, T2, and so on are contravariant)

The most frequent scenario for most users is expected to be something like this one:

While this code doesn’t look that impressive, it allows you to reuse a lot of methods that accept IEnumerable objects.

A couple of important rules to remember:
This feature works only for generic interfaces and delegates. If you implement a variant generic interface, the implementing class is still invariant. Classes and structs do not support variance in C# 4.0.
So the following doesn’t compile:

Variance is supported only if a type parameter is a reference type. Variance is not supported for value types.
The following doesn’t compile either:

Where can I find more examples of using covariance and contravariance?
I wrote a couple of MSDN topics that show how you can benefit from this new feature. They might help you better understand the principles of covariance and contravariance:
Using Variance in Interfaces for Generic Collections
Using Variance for Func and Action Generic Delegates
Also, take a look at the video How Do I: Use Covariance and Contravariance in VS 2010 Part I? by Eric Lippert.
How can I create variant generic interfaces and delegates myself?

The out keyword marks a type parameter as covariant, and the in keyword marks it as contravariant. The two most important rules to remember:
You can mark a generic type parameter as covariant if it is used only as a method return type and is not used as a type of formal method parameters.
And vice versa, you can mark a type as contravariant if it is used only as a type of formal method parameters and not used as a method return type.
For more information about variance validation, read Creating Variant Generic Interfaces and Variance in Delegates on MSDN and Eric Lippert’s post Exact rules for variance validity.
This example shows how to create a variant generic interface:

If you extend a variant interface, the extending interface is invariant by default. You must explicitly specify whether the type parameters are covariant or contravariant by using the out or in keyword. Here is a quick example from MSDN:

 

How Do I: Use Covariance and Contravariance in VS 2010 Part II?

 

http://msdn.microsoft.com/en-us/vcsharp/ee672319.aspx

Source : http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx

Polyglot & Pragmatic Programmer • Developer Advocate, IBM Cloud • Intel software Innovator • DZone MVB
(Visited 14 times, 1 visits today)

You may also like...

Show Buttons
Hide Buttons