Delegates in C# - Part 2

In my last article, Delegates in C# - Part 1, I talked about what delegates are and their advantages like:

  • Passing a method as a parameter
  • Type Safety
  • Chaining
  • Callback

Since I haven't talked about other advantages, today I will continue with the following remaining advantages of using delegates:

  • Event Handling
  • Covariance in delegates
  • Contravariance in delegates
  • Anonymous method referencing

Event handling

Another way of using a delegate is on the basis of Events. Now what is an event? We usually say that ASP.Net is an event-driven environment. The event model developed based on the two entities, Publisher and Subscriber. Publishers are those events with logic for publishing. Subscribers have events for actually subscribing to receive an event from a publisher.

Event handling

Syntax of declaring event

    access-specifier event delegate-name eventname

Covariance and Contravariance provides flexibility when you match method signatures with delegate types. Now, let us see each example of a delegate in detail with an example.
  1. public class Articles  
  2. {  
  3.     public virtual void Display()  
  4.     {    
  5.         //Code here   
  6.     }  
  7. }  
  8. public class GadgetArticles : Articles  
  9. {  
  10.     public string AuthorName { getset; }  
  11.     public override void Display()  
  12.     {  
  13.         Console.WriteLine("Features of Iphone 5 by " + AuthorName);  
  14.     }  
  15. }  
Covariance

Suppose I have a magazine with various sections of articles as shown below.

Covariance

Politics and Gadget sections are inherited from the Articles class, that is a base class, whereas GadgetArticles and PoliticsArticles are derived classes.
  1. public delegate GadgetArticles GetArticle();  
  2. class Covariance  
  3. {  
  4.     public static GadgetArticles GadgetTalk()  
  5.     {  
  6.         GadgetArticles obj = new GadgetArticles() { AuthorName = "Paul" };  
  7.         return obj;  
  8.     }  
  9.     static void Main(string[] args)  
  10.     {  
  11.         //Covariance  
  12.         GetArticle article = GadgetTalk;  
  13.         Articles obj = article();  
  14.         obj.Display();  
  15.         Console.ReadKey();  
  16.     }  
  17. }  

Output

program output

The preceding code shows how delegates can be used to return a derived type even though delegates return a base type in the signature. The return type of the "article" delegate is GadgetArticles whereas a delegate was declared with the return type Articles. So Covariance allows a method to have more derived return types than what is defined in the delegate.

Contravariance

Contravariance mainly focuses on parameters passed to a method. Contravariance allows a method with a parameter type, passed to methods that are less derived than those in the delegate type.

  1. public delegate void GetArticle(GadgetArticles art);  
  2. public class Contravariance  
  3. {  
  4.     public static void GadgetTalk(Articles gobj)  
  5.     {  
  6.         gobj.Display();  
  7.     }  
  8.     static void Main(string[] args)  
  9.     {  
  10.         //Contravariance  
  11.         GetArticle article = GadgetTalk;  
  12.         GadgetArticles gobj = new GadgetArticles() { AuthorName = "Paul"   };  
  13.         article(gobj);  
  14.         Console.ReadKey();  
  15.     }  
  16. }  
Output

program output

The preceding code shows the Contravariance in a delegate, since we are using the Articles class as a parameter of GadgetTalk instead of the GadgetArticles class as a parameter.

Referencing anonymous methods to delegate

Anonymous methods were introduced in C# 2.0 where you can create unnamed methods with a body using a delegate.

Let me give you an example of creating an anonymous method referenced by the delegate GetArticle taking a parameter of article name.

delegate

Output

Output

In the preceding code you can see I have not created any method. Instead a code block is preceded with the keyword delegate. This is called an Anonymous method.

Conclusion

Here I have completed my article. I hope the concept of delegates and its advantages is clear to you now.

I tried my best to use a simple example to explain how powerful the Delegate is. Please share your comments and thoughts for this article whether it's good or bad. At the end Sharing is valuable no matter what. 

Up Next
    Ebook Download
    View all
    Learn
    View all