Community-Credit.com | NonProfitWays.com | SOAPitstop.com   Skin:   
      User: Not logged in 
Home
Newsletter Signup
XSLT Library
Latest XML Blogs
Featured Examples
Presentations
Featured Articles
Book Chapters
Training Courses
Events
NewsGroups
 
Discussions
Examples
Tutorials
Tools
Articles
Resources
Websites
 
Sign In
My Profile
My Articles
My Examples
My Favorites
My Resources
Add a Resource
Logout
 
About Me
My Blog
HeadGeek Articles
Talking Portfolio
Resume
Pictures
World Trip Pics

Delegates in C#2.0
Author: phanindra yerra
Published: 6/13/2006 7:20 AM
Category: C#
Summary: This article explains Co Variant and Contra Variant delegates:

 

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

 

 


 

 
Fans of "The Office"
Dwight Bobbleheads are here!
  “It's me! I'm the bobblehead! Yes!”



Advertise on XMLPitstop

Advertise on XMLPitstop


EggHead Cafe
Web Servicee development
DotNetSlackers
Buy text ads here!
Color Laser Printer
online business degree
Dior sunglasses
Skype vs. sipcall
VoIP Internettelefonie
Buy text ads here!

Interested in Text ads?
2,218 Total Members
33 members(last 30 days)
6 members(last 7 days)
0 members(today)

1,609 Total Discussions
14 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

17,054 Total Blog Posts
1,739 Blogs(last 30 days)
223 Blogs(last 7 days)
0 Blogs(today)

8,699 Newsgroup Posts
0 Posts(last 30 days)
0 Posts(last 7 days)
0 Posts(today)

13,785 Total Resources
4 Resources(last 30 days)
1 Resources(last 7 days)
0 Resources(today)


 

David Silverlight's XMLPitstop.com| 2801 Florida Ave #225|Miami, FL 33133|Ph:305-447-1139