July, 2009

  • Wriju's BLOG

    C# 4.0 : Co-variance and Contra-variance

    • 4 Comments

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

     

  • Wriju's BLOG

    Welcome to Microsoft Web Platform

    • 0 Comments

     

     

    It is amazing to see so many Web Platform utilities are clubbed together in one single place. What you may need to develop website, are available here.

     

    http://www.microsoft.com/web/default.aspx

     

    If you go to downloads section http://www.microsoft.com/web/downloads/default.aspx you will get lot of things here,

    image

    Namoskar!!!

  • Wriju's BLOG

    Windows Azure : July 2009 CTP Update

    • 0 Comments

    New update for Azure Tools and SDK available for download here. I installed it and was not able to find my old set of templates there. Do not panic!!! Now it looks like,

    image

    Namoskar!!!

  • Wriju's BLOG

    “Good Bye” Thriller - Mike Jackson in Silverlight

    • 0 Comments

    King of Pop is no more available as showman to us. We will miss his presence in every bit of our life.

     

    Microsoft Silverlight is broadcasting his farewell through Silverlight at http://inmusic.ca/news_and_features/Michael_Jackson

     

    “Good Bye” King. We will miss you.

    Namoskar!!!

Page 1 of 1 (4 items)