14.3 Combining delegates


If each delegate instance is only good for encapsulating one method, then its use will be very limited. The good news is that you can have one delegate instance encapsulating multiple methods in its invocation list. [6] It does not matter whether the methods in a delegate instance's invocation list are static or not. It also does not matter if these methods are totally unrelated and belong to classes in unrelated namespaces. All that matters is that each method matches the delegate type's return type and parameters.

[6] Some books use the terms 'composite delegate' or 'multi-cast delegate' to refer to delegate instances with more than one method in their invocation list.

You can use the + and - operators (as well as += and - =) to combine delegate instances, each of which represent one method, to get a delegate instance which represents multiple methods. The statements below create delegate instances of MyDelegate called d1 and d2 :

 MyDelegate d1 = new MyDelegate(TestClass.Double); TestClass tc = new TestClass(); MyDelegate d2 = new MyDelegate(tc.Triple); 

We can 'add' d1 and d2 together to get a new delegate instance which contains both methods TestClass.Double and tc.Triple in its invocation list. The new delegate instance is called compositeDelegate :

 MyDelegate compositeDelegate = d1 + d2; 

Now, delegate instance compositeDelegate 's invocation list contains the two methods TestClass.Double and tc.Triple .

You can also do something like this to achieve the same effect:

 MyDelegate compositeDelegate =     new MyDelegate(TestClass.Double) +     new MyDelegate(tc.Triple); 

Note that the invocation list of a delegate instance is ordered. In the scenario above, TestClass.Double comes before tc.Triple on compositeDelegate 's invocation list.

When you invoke compositeDelegate with a suitable int parameter, all the methods on the invocation list are invoked in that order with the same parameter.

If the methods on the invocation list return a value, only the last invoked method's return value is returned by the invocation of the delegate instance. Figure 14.3 summarizes this idea.

Figure 14.3. A delegate instance's invocation list can contain multiple methods. When the delegate instance is invoked, the methods in its invocation list are invoked sequentially with the same parameters that were used to invoke the delegate instance.

graphics/14fig03.gif

A full coded example is shown below.

 1:  using System;  2:  3:  delegate int MyDelegate (int i);  4:  5:  class TestClass{  6:  7:    static int Double (int val){  8:      Console.WriteLine("running Double");  9:      return val*2; 10:    } 11: 12:    int Triple (int val){ 13:      Console.WriteLine("running Triple"); 14:      return val*3; 15:    } 16: 17:    public static void Main(){ 18: 19:      TestClass tc = new TestClass(); 20:      MyDelegate d1, d2, compositeDelegate; 21: 22:  d1  =  new MyDelegate(TestClass.Double);  23:  d2  =  new MyDelegate(tc.Triple);  24:  compositeDelegate  =  d1  +  d2;  25: 26:  int retVal  =  compositeDelegate(3);  27:  Console.WriteLine(retVal);  27:    } 28:  } 

Output:

 c:\expt>test running Double running Triple 9 

In line 24, a new delegate instance is created ( compositeDelegate ) which has TestClass.Double and tc.Triple in its invocation list (in that order). When invoked on line 26, TestClass.Double is invoked first with parameter 3 (which prints out " running Double "), followed by tc.Triple with the same parameter (which prints out " running Triple "). Only the return value of the last invoked method (i.e. the value 9 from tc.Triple ) is returned by compositeDelegate . This return value is then assigned to the variable retVal (line 26) and printed out (line 27).



From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net