Partager via


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!!!

Comments

  • Anonymous
    August 03, 2009
    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.

  • Anonymous
    August 03, 2009
    Nice thought MARK

  • Anonymous
    September 08, 2009
    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.

  • Anonymous
    February 15, 2010
    What if while coding I am not sure whether my delegate is going to be used for covariance or for contravariance and I would like to have both. For covariance - out For contravariance - in for both - what?