14.4 Removing delegates


As well as combining, you can also remove methods from a delegate instance using the - operator. It is possible to have a delegate instance with no method in its invocation list. However, when such a delegate instance is invoked, a System.NullReferenceException is thrown.

It is also possible to have a method in the list more than once. For example, this statement is perfectly legal:

 MyDelegate compositeDelegate = d1 + d1; 

When you attempt to remove a method which does not exist in the invocation list, nothing happens.

When you remove a method which occurs more than once in the list, the last is removed. Assuming that d1 , d2 , and d3 represent delegate instances encapsulating Method1 , Method2 , and Method3 respectively, you can put them all into compositeDelegate like this:

 MyDelegate compositeDelegate = d1 + d2 + d3 + d2 + d1; 

Invoking compositeDelegate will invoke Method1 , Method2 , Method3 , Method2 and Method1 in that order.

The statement:

 compositeDelegate -= d2; 

will result in the last Method2 (the one between d3 and d1 ) to be removed from the invocation list. Thereafter, invoking compositeDelegate will invoke only Method1 , Method2 , Method3 , and Method1 in that order.

An example is given here to clarify and summarize the concepts of combining and removing delegates discussed so far.

Assume that methods M1 , M2 , and M3 are compatible with MyDelegate and study the following code fragment. The corresponding comments show the ordered list of methods in the invocation list of compositeDelegate .

 10:  MyDelegate d1 = new MyDelegate(M1); 11:  MyDelegate d2 = new MyDelegate(M2); 12:  MyDelegate d3 = new MyDelegate(M3); 13: 14:  MyDelegate compositeDelegate = d1 + d2; // M1,M2 15:  compositeDelegate(2); // invoked in order: M1,M2 16: 17:  compositeDelegate += d3; // M1,M2,M3 18:  compositeDelegate(2); // invoked in order: M1,M2,M3 19: 20:  compositeDelegate -= d2; // M1,M3. (M2 removed) 21:  compositeDelegate(2); // invoked in order: M1,M3 22: 23:  compositeDelegate -= d2; // nothing happens. 24: 25:  compositeDelegate -= d3; // M1. (M3 removed) 26:  compositeDelegate(2); // invoked: M1 27: 28:  compositeDelegate -= d1; // nothing on list 29:  compositeDelegate(2);//throws                                  System.NullReferenceException 

Composite delegates can be very useful. Effectively, we have one delegate instance representing multiple unrelated methods. By invoking this single delegate instance, all the methods in the invocation list get invoked.

Looking at things from an event point of view, can you see that a delegate instance can be used to store event handler methods? Interested event consumers [7] can register their interest by adding their event handler method to a single delegate instance. This delegate instance is ' offered ' by an event source. [8] The event source maintains one delegate instance for each potential event, and invokes the relevant delegate instance when that particular event occurs. This will in turn invoke all the event handler methods which are in the invocation list of the relevant delegate instance.

[7] Event consumers are also known as 'event listeners' or 'event sinks' in different literature. The event consumer consumes an event. Typically, the event consumer registers with an event source about an event it is interested in. When the event occurs, the event source will notify the event consumer about the event by passing over an event object.

[8] Event sources are also known as 'event generators'. The event source generates an event object for interested event consumers who wish to be notified when that event occurs.

If you understand this, you are beginning to understand events in C#. Keep this in mind when you move on to the events chapter.



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