Information Technology Reference
In-Depth Information
Combining Delegates
All the delegates you've seen so far have had only a single method in their invocation lists.
Delegates can be “combined” by using the addition operator. The result of the operation is
the creation of a new delegate, with an invocation list that is the concatenation of copies of the
invocation lists of the two operand delegates.
For example, the following code creates three delegates. The third delegate is created from
the combination of the first two.
MyDel DelA = MyInstObj.MyM1;
MyDel DelB = SClass.OtherM2;
MyDel DelC = DelA + DelB; // Has combined invocation list
Although the term combining delegates might give the impression that the operand dele-
gates are modified, they are not changed at all. As a matter of fact, delegates are immutable .
After a delegate object is created, it cannot be changed.
Figure 15-5 illustrates the results of the preceding code. Notice that the operand delegates
remain unchanged.
Figure 15-5. Combining delegates
Search WWH ::




Custom Search