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

Why we use get and set method in c#?

495


Explain lock, monitors, and mutex object in threading.

510


What is difference between ilist and list?

483


What are the Configuration files in .net?

511


Can an array be null c#?

500






Explain the difference between a struct and a class?

502


Explain the steps to create satellite assembly?

482


Which of these string definitions will prevent escaping on backslashes in c#?

567


What is the do while loop code?

489


Can we overload the main method in c#?

476


What is datagrid c#?

472


What exactly happens when we debug and build the program?

1983


What are the 3 elements of delegation?

489


Can you use all access modifiers for all types?

537


Why do we need generics in c#?

508