Another cool thing about delegates is the ability to chain them together. Say
for example you have an object modification process, and you need a given object
to be manipulated in a very specific order. Well you could use a delegate chain
to accomplish that. For a simple example I have written up a C# delegate chain
program that evaluates a mathematical expression following the order of
operations by using a delegate chain.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
| using System;
namespace DelegateChain
{
// Declare the delegate type
delegate void UrdaDelegate(ref int x);
// A general class to store methods for use by delegates
public class DelegateMath
{
public static void AddOne(ref int a)
{
a = a + 1;
}
public static void TimesTwo(ref int b)
{
b = b * 2;
}
public static void PlusThree(ref int c)
{
c = c + 3;
}
}
public class DelegateChainExample
{
public static void Main(string[] args)
{
// Create delegate objects based on our created methods.
UrdaDelegate delegate01 = new UrdaDelegate(DelegateMath.AddOne);
UrdaDelegate delegate02 = new UrdaDelegate(DelegateMath.TimesTwo);
UrdaDelegate delegate03 = new UrdaDelegate(DelegateMath.PlusThree);
// Chain the delegates together. The variable manipulation will
// start with delegate01, then delegate02, and end with delegate03.
UrdaDelegate DelegateChain = delegate01 + delegate02 + delegate03;
// Define our value, and build the expected result from it
int value = 5;
int expected = ((value + 1) * 2) + 3;
// Build a string for explaining the output
string ExplanationString = "\n";
ExplanationString += "DelegateChain(5) should produce " + expected;
ExplanationString += " since ((" + value + " + 1) * 2) + 3 = ";
ExplanationString += expected;
// Pass the value into the delegate chain
DelegateChain(ref value);
// ...and write the explanation and result to console!
Console.WriteLine(ExplanationString);
Console.WriteLine("RESULT: " + value + "\n");
}
}
}
|
…and when we run the program we get this output:

Sure you could have just performed the mathematical expression by itself without
writing all of that code. But the point is you are able to link delegates
together, and perform manipulation on a given variable at each function in a
specific order. This can be dragged out to more complex methods, where ordered
execution is mission critical.