Covariance and Contravariance in C# 4.0 with example

C# the language, right from the very beginning is always supported covariance and contravariance but in most simple scenario, now in C# 4.0 we have full support of covariance and contravariance in all circumstances, generic interface and generic delegates, other C# types already support from first version of C#. These two terms sounds like technically scary as we will see they are incredibly simple concepts and once we will finish this, you will agree that they are not scary.

We can say C# 4.0 has not introduced new feature but what it has done is fixed a bug, it’s basically broad in two line, the way in particular generic interface and generic delegate how they work in polymorphism.

Let’s create a class Car and derived two other classes Sedan and Coupe from this class as follows:

public class Car

public class Sedan:Car

public class Coupe:Car

Now try to create object of Car and initialize with object of Sedan or Coupe

Car objcar = new Sedan();// valid statement
objcar = new Coupe(); valid statement

As you can see here we used dynamic polymorphism, till now everything is ok, a Car can be Sedan or Coupe, what about a group of Sedan? Can a group of Sedan or Coupe be a group of Car? Like this

IEnumerable<Car> cars = new List<Sedan>();
cars = new List<Coupe>();

Theoretically it should be, because if sedan is a car then a group of sedan should be a group of car but it is not true, if you will try this with C# 3.5 it will give error because C# 3.5 does not support covariance/contravariance with generic interface and generic delegate, here is the error:

Cannot implicitly convert type 'System.Collections.Generic.List< Sedan>' 
to 'System.Collections.Generic.IEnumerable< Car>'. An explicit conversion exists 
(are you missing a cast?)

Open project property and select Application tab and change the target framework to 4.0 and now the same code will work because covariance & contravariance with generic interface and generic delegate is supported by framework 4.0.

Ali Adravi Having 13+ years of experience in Microsoft Technologies (C#, ASP.Net, MVC and SQL Server). Worked with Metaoption LLC, for more than 9 years and still with the same company. Always ready to learn new technologies and tricks.
  • c#
  • oops
By Ali Adravi On 29 Mar, 13  Viewed: 4,988

Other blogs you may like

Polymorphism in C# with example

Polymorphism is derived from two Latin words, 1. Poly means many 2. Marphose means forms. By using inheritance, a class can be used as many types, say we have a Person which may be a Professor, may be a Student, may be Clerk. So in this article we will see some example of Polymorphism. When we... By Hamden   On 01 Jun 2013  Viewed: 4,564

How to create windows service in C# visual studio 2010

Creating windows service in C# is really easy but when we need to create first time we face many problems so in this post we will try to learn everything about window service. In this article we will create a windows service which will run after a particular interval, and time should be... By Ali Adravi   On 30 Apr 2013  Viewed: 8,641

default value for different data type in C#

In C# there are different data type and they use some default value when we declare a variable. When we define a variable of type int or Int32 say int score; so what is the value of score, will it be null or zero, in the same way if we create a variable of type string/String what value it holds... By Ali Adravi   On 27 Mar 2013  Viewed: 2,615

enum in C# and conversion

An enumeration type (also known an enumeration or an enum) provides an efficient way to define a set of named integral constants that may be assigned to a variable to make programming clear, understandable and manageable. There are ways of using, like how to use enum in switch and case statement,... By Hamden   On 27 Mar 2013  Viewed: 4,571

Dynamic vs var in C# with example

When we have var which dynamically holds any type or value so why we need dynamic type, what are the differences and situations where we can use dynamic rather than var, there were some question in my mind so I explored more which I want to share here. There are two things, static type and... By Nathan Armour   On 26 Mar 2013  Viewed: 3,219