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



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

Answer / 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

More C Sharp Interview Questions

How is the memory managed in C#.

1 Answers  


What are floating point numbers?

0 Answers  


What is a system lock?

0 Answers   KEN Group, Wipro,


What is generic in c#?

0 Answers  


How many aware interfaces are there?

0 Answers  






what is partial assembly reference

0 Answers  


What is literal in c#?

0 Answers  


how to Create a datagridview control with check box column with 8rows in it, the maximum number of check boxes checked should be 3, when user checks the 4th corresponding message should be displayed and check box should be checked. User can uncheck the checked boxes Note: read-only property should not be used

0 Answers   HCL, NIC,


Why are there five tracing levels in System.Diagnostics.TraceSwitcher?

1 Answers  


What is the c# equivalent of c++ catch (...), Which was a catch-all statement for any possible exception?

0 Answers  


Explain About the Sattilite Assembly in .Net Technology?

2 Answers   TCS,


What does mean before a string in c#?

0 Answers  


Categories