Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
C# 4.0 : Co-variance and Contra-variance

Here I am going to talk about the feature called Covariance and Contravariance. To show you the example I will use delegate. Now what is it? This would be a big question and with no hesitance I must say that you should visit Eric’s blog. There are some articles which will help you understand.

To me when you can assign base type to a delegate type. It was there in earlier versions of C# by default. But now the tread will follow for delegates and interfaces for Generic types.

CoVariance

Now if you have a class hierarchy, Parent and Child (as name mentioned). Now ideally you can assign Child to any Parent object as Parent is larger type and hold properties of Child. Now if you try to write come code as mentioned below,

namespace CoVarinace

{

    class Vegetable { }

    class Potato : Vegetable { }

 

    class Program

    {

        delegate T MyFunc<T>();

 

         static void Main(string[] args)

         {

             //Covariance

             MyFunc<Potato> potato1 = () => new Potato();

 

             MyFunc<Vegetable> veg = potato1;

         }

    }

}

This code will fail with the below error message

“Cannot implicitly convert type ' CoVarinace.Program.MyFunc<CoVarinace.Potato>' to 'CoVarinace.Program.MyFunc<CoVarinace.Vegetable>'     C:\..\CoVarinace\Program.cs” 

Now in Visual Studio 2010 with a minor change this code will work.

delegate T MyFunc<out T>();

Now with the modified code it will look like,

CoVariance

namespace CoVarinace

{

    class Vegetable { }

    class Potato : Vegetable { }

 

    class Program

    {

        delegate T MyFunc<out T>();

 

         static void Main(string[] args)

         {

             //Covariance

             MyFunc<Potato> potato1 = () => new Potato();

 

             MyFunc<Vegetable> veg = potato1;

         }

    }

}

 

 

With no error, the support is only from Visual Studio 2010.

ContraVariance

Contravariance is the most confusing thing to understand to me. Till today it is not that clear. I promise to comeback with better explanation in near future. Now if you think other way around, you surely cannot have assignment of bigger thing to smaller. But again for Generic delegates in Visual Studio 2010 it is supported. This works with generic delegate which returns nothing but takes a parameter.

If you write the code,

namespace ContraVarinace

{

    class Vegetable { }

    class Potato : Vegetable { }

 

    class Program

    {

        delegate void MyAction<T>(T a);

 

        static void Main(string[] args)

        {

            MyAction<Vegetable> action1 = (veg) => {Console.WriteLine(veg); };

 

            MyAction<Potato> potato1 = action1;

        }

    }

}

This will throw you an error,

Cannot implicitly convert type 'ContraVarinace.Program.MyAction<ContraVarinace.Vegetable>' to ' ContraVarinace.Program.MyAction<ContraVarinace.Potato>'   C:\..\ContraVarinace\Program.cs

With a little modification the below code will work without any issue, you just need to put the keyword in.

delegate void MyAction<in T>(T a);

ContraVariance

namespace ContraVarinace

{

    class Vegetable { }

    class Potato : Vegetable { }

 

    class Program

    {       

        delegate void MyAction<in T>(T a);

 

        static void Main(string[] args)

        {

            MyAction<Vegetable> action1 = (veg) => { Console.WriteLine(veg); };

 

            MyAction<Potato> potato1 = action1;

        }

    }

}

 

Namoskar!!!

 

Posted: Friday, July 31, 2009 4:37 PM by wriju

Comments

Mark Sowul said:

Easy example for contravariance is Equals or CompareTo; if you have a function to compare Vegetables, you can use it to compare Potatoes as well.

# August 3, 2009 9:21 AM

wriju said:

Nice thought MARK

# August 3, 2009 8:26 PM

Ashu said:

You Have Given A good Example But Haven't Specified the concept behind the Co varience and contra varience

where to use this and in which senario it is used.

# September 9, 2009 7:50 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker