A useful feature of delegates is that delegates can point to more than one method. A delegate that points to multiple methods is called Multicast Delegate. When invoking Multicast Delegate all methods that he points to are invoked. We add methods to delegate by using + or += operator. Methods can be removed from delegate by using – or -= operators.
When delegate has return type void, all methods are executed. The same is with all other return types. Difference is that when return type is not void, we will receive only value of last method executed.
Examples of Multicast Delegate using void return type:
Add methods to delegate by using + operator
We will create console application that will print content of four methods by using delegates.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public delegate void VoidDelegate(); class App { private static void Main() { VoidDelegate voidDelegate1, voidDelegate2, voidDelegate3, voidDelegate4, voidDelegate5; voidDelegate1 = new VoidDelegate(ShowMessageOne); voidDelegate2 = new VoidDelegate(ShowMessageTwo); voidDelegate3 = new VoidDelegate(ShowMessageThree); voidDelegate4 = new VoidDelegate(ShowMessageFour); voidDelegate5 = voidDelegate1 + voidDelegate2 + voidDelegate3 + voidDelegate4; voidDelegate5(); } public static void ShowMessageOne() { Console.WriteLine("ShowMessageOne method invoked."); } public static void ShowMessageTwo() { Console.WriteLine("ShowMessageTwo method invoked."); } public static void ShowMessageThree() { Console.WriteLine("ShowMessageThree method invoked."); } public static void ShowMessageFour() { Console.WriteLine("ShowMessageFour method invoked."); } } |
First we created our new delegate VoidDelegate that has return type void and takes no input parameters. Then, inside our Main method, we defined 5 VoidDelegates, and instantiate four of the them. After we are done with instantiating 4 delegates, we assign those 4 delegates using + operator to fifth delegate. Lastly, we invoke the delegate. Console output will show all 4 ShowMessage methods results.
Remove methods from delegate by using – operator
Let’s rework our previous code so we can see example of removing methods from a delegates by using – operator. Next example will show you only Main method, because no changes are needed to be maid for ShowMessage methods.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
private static void Main() { VoidDelegate voidDelegate1, voidDelegate2, voidDelegate3, voidDelegate4, voidDelegate5; voidDelegate1 = new VoidDelegate(ShowMessageOne); voidDelegate2 = new VoidDelegate(ShowMessageTwo); voidDelegate3 = new VoidDelegate(ShowMessageThree); voidDelegate4 = new VoidDelegate(ShowMessageFour); voidDelegate5 = voidDelegate1 + voidDelegate2 + voidDelegate3 + voidDelegate4; voidDelegate5(); Console.WriteLine("***"); voidDelegate5 = voidDelegate5 - voidDelegate1; voidDelegate5(); } |
First part of a code is same as in previous example, but later on we just use – operator to unsubscribe voidDelegate1 delegate (that uses ShowMessageOne method) from voidDelegate5 delegate. Note that ShowMessageOne message is not shown.
Add methods to delegate by using += operator
1 2 3 4 5 6 7 8 9 |
private static void Main() { VoidDelegate voidDelegate = new VoidDelegate(ShowMessageOne); voidDelegate += ShowMessageTwo; voidDelegate += ShowMessageThree; voidDelegate += ShowMessageFour; voidDelegate(); } |
Seems that += operators shortened our code and made it a lot readable. This time we needed to declare only one delegate and add methods to him by only using += operators.
Remove methods from delegate by using -= operator
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
private static void Main() { VoidDelegate voidDelegate = new VoidDelegate(ShowMessageOne); voidDelegate += ShowMessageTwo; voidDelegate += ShowMessageThree; voidDelegate += ShowMessageFour; voidDelegate(); Console.WriteLine("***"); voidDelegate -= ShowMessageOne; voidDelegate(); } |
Example of Multicast Delegate using string return type
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
public delegate string StringDelegate(); class App { private static void Main() { StringDelegate stringDelegate = new StringDelegate(ShowMessageOne); stringDelegate += ShowMessageTwo; stringDelegate += ShowMessageThree; stringDelegate += ShowMessageFour; string message = stringDelegate(); Console.WriteLine(message); } public static string ShowMessageOne() { return "ShowMessageOne method invoked."; } public static string ShowMessageTwo() { return "ShowMessageTwo method invoked."; } public static string ShowMessageThree() { return "ShowMessageThree method invoked."; } public static string ShowMessageFour() { return "ShowMessageFour method invoked."; } } |
While running this app, try entering debug mode, you will see that all methods were hit but only ShowMessageFour message was sent. Remember that while consuming your delegates.
We will continue delegates discussion with Anonymous methods.
22/02/2017 at 19:13
You actually make it appear so easy along with
your presentation however I find this matter to be actually something which I think I’d never understand.
It sort of feels too complicated and very large for me.
I’m looking forward to your subsequent publish, I will attempt to get the grasp of it!
02/03/2017 at 14:27
I will try to make another tutorial for you that will go a bit into depth why and how we use Delegates in real life with some useful scenarios. Visit this site next week and maybe you will get better grasp of delegates. Also this weekend there will be cool post about Dapper ORM.