C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Covariant and contravariant delegates


Posted Date: 04 May 2006    Resource Type: Articles    Category: .NET Framework
Author: phanindra yerraMember Level: Gold    
Rating: Points: 5



Introduction



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.

Covariant 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 variant 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);
}
}



Summary



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.







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Boxing and UnBoxing in C#
Previous Resource: How to Change the Image in datagrid based on a field in database
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design


Contact Us    Privacy Policy    Terms Of Use