What are Covariance and ContraVariance in C#4.0?


In this whole artcile we will learn all about Covariance and Contracvariance, what were the issues with development before these two.


These two Covariance and Contravariance have been introduced in C#4.0. As per msdn
we simply define:

covariance and contravariance enable implicit reference conversion for array types, delegate types, and generic type arguments. Covariance preserves
assignment compatibility and contravariance reverses it.

In simple words, we can say Covariance and Contravariance are the polymorphism extensions to array types, delegate types, and generic type (we will see in coming sections).

Issue before these two

What were the issues before the invension of these two. lets consider following example:

class GrandFather


class Son : GrandFather


class GrandSon : GrandFather


you can see we are using inheritance, GrandFather is a parent class to Son and GrandSon. As per polymorphism we can do this:

GrandFather father = new Son();
father   = new GrandSon();

if above statement is correct then following statement also should be correct:

//will throw exception prior to version 4.0
IEnumerable<GrandFather> fathers = new List<Son>();

isn’t it somehow violate polymorphism 🙂

Now, try this in C#4.0

//will work fine with version 4.0
IEnumerable<GrandFather> fathers = new List<Son>();

in above IEnumerable, an out parameter of type T.

Consider following method is in the class:

static void ParentalObject(object o)
	//method signature
Action<Object> actionObj = ParentalObject;

and we can assign this instantiated object to

Action<Object> actionObj1 = actionObj;

intatiated object with more derived type arguments

Array Covariance

This is nothing but implicit conversion of an array of a more derived type to an array of a less derived type. Unfortunately this is not type safe.

object[] strArray = new string[10];
strArray[0] = 1; //will throw runtime exception

In above example, our object array is of string type but we are assigning integer value to one of its element, it will not throw any compile-time exception
but, it will throw runtime exception.

[h2>Delegate Covariance

This type of Covariance is also called as method group variance. From msdn
you can assign to delegates not only methods that have matching signatures, but also methods that return more derived types (covariance) or that accept
parameters that have less derived types (contravariance) than that specified by the delegate type. This includes both generic and non-generic delegates.

static string Parental()
	return "Grandfather name is: Lala Bhagwan Das";

and see following:

Func<object> parentalLambda = () => Parental(); //lambda expression
Func<object> parentalFunc = Parental; //Grouped

Now, consider following method in a class:

static string ParentalObject(object obj)
	//method signature

Contravariance. A delegate specifies a parameter type as string, but can assign a method that takes an object.

Action<String> parentalDel = ParentalObject;
[h2>Variance in Generic Type Parameters

As per msdn you can enable implicit conversion between delegates, so that generic delegates that have different types specified by
generic type parameters can be assigned to each other, if the types are inherited from each other as required by variance.

To enable implicit conversion, you must explicitly declare generic parameters in a delegate as covariant or contravariant by using the in or out keyword.

Consider below code:

delegate T ParentalGenericDelegate<out T>();

In above we declated type T as a contravariant by using the out keyword.

ParentalGenericDelegate<string> grandFatherName = () => "Grandfather name is: Lala Bhagwan Das";

Above, shows how we can create a delegate which is having generic type parameter.

ParentalGenericDelegate<object> anotherGenericDelegate = () => grandFatherName;

Above statement is correct, we can assign delegates to each-other and type T is declared as covariant.


We can say Covariance and Contravariance are the polymorphism extensions to array types, delegate types, and generic type (we will see in coming sections).