Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is covariance and contravariance? Did Delegate and
method overriding support these?

Answer Posted / anil

What’s New – C# 2.0 Series
In this C# what’s new series, we will introduce and discuss
the new editions in C# 2.0 programming language. We will
explain the new features like generics, partial classes,
object oriented and other language enhancements and
demonstrate it with sample source code. The series is most
suitable for people who have worked with C# in Visual
Studio.Net o r Visual Studio.Net 2003. However, people
coming from different background like VB.Net, C++ and Java
will also find it useful if they are planning to learn or
move to C# in near future.

New Delegate Features – Covariance and Contra-variance
With C# programming language 2.0, two very useful features
regarding delegates are introduced: covariance and
contra-variance. Before we go on to explaining these lets
have a bit of review. Suppose we have two classes A and B
with A being the parent class of B

class A
{
}
class B : A
{
}



In our program, if we declare a delegate whose return type is A

class Program
{
public delegate A MyDelegate();



Then with C# 1.x, we can instantiate this delegate with a
method whose signature is exactly the same as the delegate,
i.e., a no-argument method returning an object of type A

class Program
{
public delegate A MyDelegate();
static void Main(string[] args)
{
MyDelegate delObj = new MyDelegate(Fun);

A a = delObj();
}
public static A Fun()
{
return new A();
}
}



Covariance
C# 2.0 provides us flexibility that now when instantiating a
delegate the specified method may also have the return type
which is the derived form of the return type specified by
delegate. For example, now we can instantiate the MyDelegate
with a method whose return type is B

class Program
{
public delegate A MyDelegate();
static void Main(string[] args)
{
MyDelegate delObj = new MyDelegate(Fun);

A a = delObj();
}
public static B Fun()
{
return new B();
}
}



This is called the covariance. It is not a problem for a
compiler also because B (being sub-class of A) can easily be
casted to A

Contra-variance
It even surprised me second time when studying
contra-variance as it worked contrary to what I expected,
but both of the times I realized it ought to be like this!
It really is a very interesting and fun to learn
contra-variance! So lets start now. Suppose we have two
classes A and B with A being the parent class of B like

class A
{
public void AFun()
{
Console.WriteLine("A Fun called...");
}
}
class B : A
{
public void BFun()
{
Console.WriteLine("B Fun called...");
}
}



And in our program we defined a delegate which takes a
parameter of type B

class Program
{
public delegate void MyDelegate(B bObj);



Then with C# 1.x, we can instantiate the delegate whose
signature is exactly same as that of MyDelegate() that is
which takes an object of type B

class Program
{
public delegate void MyDelegate(B bObj);
static void Main(string[] args)
{
MyDelegate delObj = new MyDelegate(Fun);

delObj(new B());
Console.ReadLine();
}
public static void Fun(B bObj)
{
bObj.BFun();
}
}



When we run the program it prints the expected output

B Fun called...

But in C# 2.0, thanks to the contra-variance feature, we can
also instantiate the delegate with a method which accepts
the parameter of parent type of the type specified by the
delegate. For instance in our example, we can instantiate
MyDelegate() with a method which accepts an object of type A!

class Program
{
public delegate void MyDelegate(B bObj);
static void Main(string[] args)
{
MyDelegate delObj = new MyDelegate(MoreFun);

delObj(new B());
Console.ReadLine();
}
public static void Fun(B bObj)
{
bObj.BFun();
}
public static void MoreFun(A aObj)
{
aObj.AFun();
}
}



When we run the program, the output is

A Fun called...

If you have as little IQ as me (which is not the case in
general) then you might be wondering how it became possible.
In fact, it is possible because the method being referred
MoreFun() accepts an object of type A but the delegate
accepts an object of type B. Since, B is a sub-class of A it
is possible to cast an object B to type A and pass it to
MoreFun().

Anyhow, covariance and contra-variance are really very
useful features and it is really good to see these features
added to the C# 2.0 programming language.

Is This Answer Correct ?    6 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is difference between destruct or and garbage collection ?

875


What is the use of constructor in c# with example?

796


What is parallel foreach c#?

899


What is private static in c#?

857


Is c# different than c++?

837


Can we declare class as protected?

920


Is a structure a class?

896


What is called method in c#?

902


Explain the security with aop?

1006


Can dictionary have duplicate keys c#?

824


What types of object can I throw as exceptions?

886


What is a private class in c#?

1023


Explain nullable types in c#?

915


What is an event in c#?

848


List out the differences between array and arraylist in c#?

913