User Name:

User Email:

This information will only be saved for the purposes of communicating with those who have provided this information voluntarily regarding our services.We will never sell your name or email address to anyone.
© 2021 - First Crazy Developer (Abhishek Kumar)

crazydeveloper What is the use of "Covariance & Contravariance" in .Net Framework 4.0

Hello, and first of all a big thanks to all of you who provided comments and feedback on my previous post.  It encouraged me to work on this one!


I’d like to discuss a couple of integral concepts in C# 4.0, "Covariance and Contravariance". In order to understand the importance of these concepts and why they exist, we'll need to hop into our time machine and take a look at some code from .NET 3.5.

First, we create a new console application with the name "CovarianceandContravarianceSample" in.Net 3.5 framework:

In the above image, we see that I’ve selected .NET 3.5 and the ‘Console Application’ template.  Now let’s take a look at the following code:

  1. class Program
  2. {
  3. class Vehicle
  4. {
  5. }
  6. class Car:Vehicle
  7. {
  8. }
  9. class Bike:Vehicle
  10. {
  11. }
  12. static void Main(string[] args)
  13. {
  14. }
  15. }

In the above code we can see there’s a class called "Vehicle".  We then create two more classes called "Car" & "Bike" which both inherit from Vehicle".  So now we have concrete classes "Car" & "Bike" with the parent class "Vehicle".

Using this parent/child relationship, let’s implement some dynamic polymorphism.  Dynamic polymorphism means we create the parent object and point the child object to it during run time as opposed to compile time.  Now, look at the following code:

  1. static void Main(string[] args)
  2. {
  3. Vehicle parentObj = new Car(); // Valid Statement
  4. parentObj = new Bike(); // Valid Statement
  5. }

I created an object of "Vehicle" named 'parentObj, then pointed the object to Car. We know the syntax here is perfectly valid. I then point to the same object with "Bike".  We know this is also a perfectly valid statement.  So, now we can build and run the above code.

Now using this valid statement as a base we write some more code, but first, here’s an important question:

We know that ‘Vehicle’ can point to ‘Car’ but can a GROUP of Vehicles point to a GROUP of cars?

When we tried into our code what will happen:

  1. static void Main(string[] args)
  2. {
  3. Vehicle parentObj = new Car(); // Valid Statement
  4. parentObj = new Bike(); // Valid Statement
  5. IEnumerable<Vehicle> listParentObj = new List<Car>();
  6. }
  7. }

When we write the above code, we get a conversion type error.  Now, this is strange and confusing for us.  I mean to say, if "Vehicle" can point to the "Car", then why can’t a group of "Vehicle" point to the group of "Car?  Here’s where "Covariance" comes into play…

Covariance allows us to do things in our code that we previously couldn’t and it is one of the many great additions to .NET 4.0

If I change the version of my project to .NET 4.0 and build the above code it should run successfully.

So, the question still remains: how the heck does it compile?!  Out of curiosity, if we check the details of the IEnumerable interface we get "<out T>" - the type of objects to enumerate.  This type parameter is covariant.  In other words we can say that the IEnumerable Interface is now covariant-enabled.  Covariance helps to maintain assignment compatibility between parent and child relationship during dynamic polymorphism.  So as a programmer, things that work logically with a single object assignment will also work with Interface collections like IEnumerable which are covariant-enabled.  This is done by using the "out" keyword.

In the next blog, we’ll look at Contravariance, which in a nutshell is exactly the opposite of Covariance

In short word we can explain that "Contravariance is exactly opposite of Covariance."

Happy reading!

Happy reading!


crazydeveloper Home Page 22 July 2015

Become a Fan