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.
Syntax of declaring event
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.
- public class Articles
- {
- public virtual void Display()
- {
-
- }
- }
- public class GadgetArticles : Articles
- {
- public string AuthorName { get; set; }
- public override void Display()
- {
- Console.WriteLine("Features of Iphone 5 by " + AuthorName);
- }
- }
Covariance
Suppose I have a magazine with various sections of articles as shown below.
Politics and Gadget sections are inherited from the Articles class, that is a base class, whereas GadgetArticles and PoliticsArticles are derived classes.
- public delegate GadgetArticles GetArticle();
- class Covariance
- {
- public static GadgetArticles GadgetTalk()
- {
- GadgetArticles obj = new GadgetArticles() { AuthorName = "Paul" };
- return obj;
- }
- static void Main(string[] args)
- {
-
- GetArticle article = GadgetTalk;
- Articles obj = article();
- obj.Display();
- Console.ReadKey();
- }
- }
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.
- public delegate void GetArticle(GadgetArticles art);
- public class Contravariance
- {
- public static void GadgetTalk(Articles gobj)
- {
- gobj.Display();
- }
- static void Main(string[] args)
- {
-
- GetArticle article = GadgetTalk;
- GadgetArticles gobj = new GadgetArticles() { AuthorName = "Paul" };
- article(gobj);
- Console.ReadKey();
- }
- }
OutputThe 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.
OutputIn 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.
ConclusionHere 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.