Co Variant and Contra Variant delegates:
This is the new feature in c# 2.0 . Covariance and contravariance provide a great degree of flexibility when matching delegate methods with delegate signatures. Covariance allows a method with a derived return type to be used as the delegate, and contravariance allows a method with derived parameters to be used as the delegate. This ability enables the creation of more flexible delegate methods.
Co Variant delegates:
When a delegate method has a return type that is more derived than the delegate signature, it is said to be covariant. Because the method's return type is more specific than the delegate signature's return type, it can be implicitly converted. The method is therefore acceptable for use as a delegate.
Covariance enables delegate methods to be created that can be used by both classes and derived classes.
This example demonstrates how delegates can use derived return types. The data type returned by Eat() is of type Dog, which is a subset of the delegate signature return type Animal. Therefore, all the possible values returned by Run() could be stored in a variable of type Animal.
Example:
class Animal
{
}
class Dog : Animal
{
}
class DelegateDemo
{
// Define the delegate.
public delegate Animal getAction();
public static Animal Run()
{
return null;
}
public static Dog Eat()
{
return null;
}
static void Main(string[] args)
{
// this delegate holds a Run() method reference this is like normal delegates because the //return type of delegate and method is same.
getAction g1 = new getAction(Run);
// Covariance feature allows this delegate. Eat() method return type is Dog which is // // //derived from Animal
getAction g2 = new getAction(Eat());
}}
I think this example gives an idea of what is co variant delegates, how delegates can use parameters of a type that are derived from the delegate signature parameter type.
Contra Variance Delegates:
When a delegate method signature has one or more parameters of types that are derived from the types of the method parameters, that method is said to be contravariant. As the delegate method signature parameters are more specific than the method parameters, they can be implicitly converted when passed to the handler method.
Contravariance therefore makes it easier to create more general delegate methods that can be used with a larger number of classes
class Animal
{
}
class Dog : Animal
{
}
class DelgateDemo
{
public delegate void getAction(Dog dog);
public static void Run(Animal cat)
{
}
public static void Eat(Dog petdog)
{
}
static void Main(string[] args)
{
//this delegate hold the method reference of methods Run() with parameters of Animal type. This is what the Contravariance delegate is !
getAction g1 = new getAction(Run);
getAction g2 = new getAction(Eat);
}
}
Covariant allows delegate can hold the reference of method even if the return types of that method is derived from return type of the delegate.
Contravariant allows delegate can hold the reference of method even if the parameters of delegate is derived from method parameters that it is holding.
More on this covariant and contravariance
http://msdn.microsoft.com/msdnmag/issues/05/10/netmatters/default.aspx