C# 4.0 Feature - CoVariance and ContraVariance
Lets see the new feature in C# 4.0 “CoVariance and ContraVariance". This is a very tricky concept and there are many illustrations in the web.
If we had to do the following prior to C# 4.0
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate T GetDriver();
static void Main(string[] args)
{
GetDriver getLaptopDriver = () => new LaptopDriver();
GetDriver getDrivers = getLaptopDriver;
Console.ReadLine();
}
We would have got a error as follows
Cannot implicitly convert type 'ConsoleApplication2.Program.GetDriver' to 'ConsoleApplication2.Program.GetDriver'
To overcome this in C# 4.0 we have CoVariance which is mainly used for assignment compatability. We need to modify the code by adding a “out” keyoword in the delagate declaration as seen below
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate T GetDriver();
static void Main(string[] args)
{
GetDriver getLaptopDriver = () => new LaptopDriver();
GetDriver getDrivers = getLaptopDriver;
Console.ReadLine();
}
Now lets see the what is Contravariance ? Lets see the below example if tried in C# 3.0
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate void GetDriver(T driver);
static void Main(string[] args)
{
GetDriver getDrivers = (drivers) => Console.WriteLine("Drivers Initialised");
GetDriver getDesktop = getDrivers;
}
We would have got the error as follows
Cannot implicitly convert type 'ConsoleApplication2
.Program.GetDriver' to 'ConsoleApplication2.Program.GetDriver'
To Overcome this in C# 4.0, we have Contravariance where we use “in” keyword in the delagate declariation which does the opposite direction of assignment.
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate void GetDriver(T driver);
static void Main(string[] args)
{
GetDriver getDrivers = (drivers) => Console.WriteLine("Drivers Initialised");
GetDriver getDesktop = getDrivers;
}
Hope this was cool feature. Please add your comments or suggestions.
Cheers!!!
If we had to do the following prior to C# 4.0
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate T GetDriver
static void Main(string[] args)
{
GetDriver
GetDriver
Console.ReadLine();
}
We would have got a error as follows
Cannot implicitly convert type 'ConsoleApplication2.Program.GetDriver
To overcome this in C# 4.0 we have CoVariance which is mainly used for assignment compatability. We need to modify the code by adding a “out” keyoword in the delagate declaration as seen below
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate T GetDriver
static void Main(string[] args)
{
GetDriver
GetDriver
Console.ReadLine();
}
Now lets see the what is Contravariance ? Lets see the below example if tried in C# 3.0
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate void GetDriver
static void Main(string[] args)
{
GetDriver
GetDriver
}
We would have got the error as follows
Cannot implicitly convert type 'ConsoleApplication2
.Program.GetDriver
To Overcome this in C# 4.0, we have Contravariance where we use “in” keyword in the delagate declariation which does the opposite direction of assignment.
class Drivers { }
class LaptopDriver : Drivers { }
class DesktopDriver : Drivers { }
class LanDriver : LaptopDriver { }
delegate void GetDriver
static void Main(string[] args)
{
GetDriver
GetDriver
}
Hope this was cool feature. Please add your comments or suggestions.
Cheers!!!
Comments