Covariance and ContraVariance in C# 4.0

This article explains the following points:

  • Introduction
  • Definition
  • Issue before these two
  • Array Covariance
  • Delegate Covariance
  • Variance in Generic Type Parameters
  • Conclusion

Introduction

In this entire article we will learn all about Covariance and Contravariance, including what the issues were with development before these two.

Definition

These two, Covariance and Contravariance, wre introduced in C# 4.0. As per MSDN we simply define them as:

"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 types (we will see in future sections).

Issue before these two

What were the issues before the invension of these two? Let's consider the following example:

  1. class GrandFather    
  2. {    
  3.      
  4. }    
  5.      
  6. class Son : GrandFather    
  7. {    
  8.      
  9. }    
  10.      
  11. class GrandSon : GrandFather    
  12. {    
  13.     
  14. }    
You can see we are using inheritance, GrandFather is a parent class to Son and GrandSon. As per polymorphism we can do this:

 

  1. 1. GrandFather father = new Son();   
  2. 2. father = new GrandSon();   
If the preceding statement is correct then the following statement also should be correct:
  1. //will throw exception prior to version 4.0   
  2. IEnumerable<GrandFather> fathers = new List<Son>();   
It is not somehow violating polymorphism.

Now, try this in C# 4.0:
  1. //will work fine with version 4.0   
  2. IEnumerable<GrandFather> fathers = new List<Son>();   
In the preceding is an IEnumerable, an out parameter of type T.

Consider the following method is in the class:
  1. static void ParentalObject(object o)   
  2. {   
  3.    //method signature   
  4. }   
  1. Action<Object> actionObj = ParentalObject;   
And we can assign this instantiated object to:
  1. Action<Object> actionObj1 = actionObj;   
The intantiated object with more derived type arguments.

Array Covariance

This is nothing but an implicit conversion of an array of a more derived type to an array of a less derived type. Unfortunately this is not type-safe.
  1. object[] strArray = new string[10];   
  2. strArray[0] = 1; //will throw runtime exception   
In the preceding example, our object array is of string type but we are assigning an integer value to one of its elements, it will not throw any compile-time exception, but it will throw a runtime exception.

Delegate Covariance

This type of Covariance is also called 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.
  1. static string Parental()   
  2. {   
  3.    return "Grandfather name is: Lala Bhagwan Das";   
  4. }   
And see the following:
  1. Func<object> parentalLambda = () => Parental(); //lambda expression   
  2. Func<object> parentalFunc = Parental; //Grouped   
Now, consider the following method in a class:
  1. static string ParentalObject(object obj)   
  2. {   
  3.       //method signature   
  4. }   
Contravariance

A delegate specifies a parameter type as a string but can assign a method that takes an object.
  1. Action<String> parentalDel = ParentalObject;   
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 using the in or out keyword.

Consider the following code:
  1. delegate T ParentalGenericDelegate<out T>();   
In the preceding we declared a type T as a contravariant using the out keyword.
  1. ParentalGenericDelegate<string> grandFatherName = () => "Grandfather name is: Lala Bhagwan Das";   
The preceding shows how to create a delegate that has a generic type parameter.
  1. ParentalGenericDelegate<object> anotherGenericDelegate = () => grandFatherName;   
The preceding statement is correct, we can assign delegates to each other and type T is declared as covariant.

Conclusion

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

 

Up Next
    Ebook Download
    View all
    Learn
    View all