.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Melody Anderson

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

C# 4.0 : Contra and Co-Variance

Posted By:Baimey Rajesh       Posted Date: August 29, 2012    Points: 25    Category: C#    URL: http://www.dotnetspark.com  

Covariance and Contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations (such as parameters, generics, and return types).

Let us see what this is:

Wikipedia says

‘Covariance and Contravariance refers to the ordering of types from narrower to wider and their interchangeability or equivalence in certain situations (such as parameters, generics, and return types).’


In C#, let us see how different these terms are.

In C#, from the beginning variance is supported in the following scenarios:

1.        Covariance in arrays (since C# 1.0)

2.        Covariance and contravariance in delegates, also known as “method group variance” (since C# 2.0).

Delegate constructor can take as a param

·         not only a method with same signature

·         but with signature that is co- or contravariative to delegate’s.

3.        Variance for generic type parameters in interfaces and delegates (since C# 4.0)


Co - Variance

Let us see some coding to make the concept clearer,

object[] objectArray = new string[] { "abc", "def" };

The above code is valid.

But again acode that appers similar,

objectArray[0] = "string 3";

this code gives an ArrayTypeMismatchException.


So we can say that arrays are not safely covariant. What can we do to make this covariant?

out generic modifier… this helps in accomplishing this task. To do this the IEnumerable interface becomes.

public interface IEnumerable<out T>
    IEnumerator<T> GetEnumerator();
public interface IEnumerator<out T>
    T Current { get; }
    bool MoveNext();

That doesn't hurt type safety at all, but lets you return anIEnumerable<string> from a method declared to return IEnumerable<object> for instance.


Contra – Variance

Contra-variance is harder to give concrete examples for using interfaces, but it's easy with a delegate. Contra-variance is the dual of covariance. In that a type argument can be safely replaced by a more derived type but only in an input position.

in generic modifier…

 It would be nice to be able to convert seamlessly use an Action<object> as an Action<string> - any method which takes an object parameter is going to be fine when it's presented with a string instead.


In the sample T is contravariant as it uses ‘in’ Keyword

public interface IAdd<in T>
    int Add(T a, T b);

Now let's combine both co and contravariance, consider the following delegate


public delegate T2 Func<in T1,out T2>(T1 t1);

T2 is covariant and T1 is contravariant and hence we can use T1 or any subtypes of T1 as the first generic type paramter and T2 or any of its base types as the second generic type parameter, allowing us to use assignments such as the following with full type safety 

More in-depth information about covariance and contravariance:

This is the MSDN root topic: Covariance and Contravariance.

And read Eric Lippert’s blog. He designed this feature for C# 4.0, so who knows more about it?


Covariance and Contravariance enables writing generic code more naturally and allows safe type coercions. C# 4 will make this more generic, and (I believe) will avoid creating a new instance for the conversion. (It'll be a reference conversion instead.)


Hope it is clear

Happy Coding J



 Subscribe to Articles


Further Readings:


    No response found. Be the first to respond this post

    Post Comment

    You must Sign In To post reply
    Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend