Comparing delegates

delegates can be compared. A delegate overrides the equality operator (==) and the (!=) operator. These two operators in turn rely on the Equals method that is an overridden method for the delegate class. Similar to the way strings can be compared by value and not by reference, the Delegate class compares the value of the delegate rather than the reference. For a delegate, the value consists of the values exposed by the Method and Target properties seen in Listings 14.5 and 14.8. If the Method and Target properties are the same, then the delegates are considered to be equal. Two different instances will obviously have different Targets, so delegates defined on different instances will be considered different. In addition, if a delegate is defined as static, it will always be different from an instance delegate because the Target for a static delegate is essentially always null. A couple of examples will help to clarify the concept. The source code for this sample illustrated in Listings 14.1014.14 is in the DelegateEquality directory.

Listing 14.10. Comparing Static Methods
DelegateCallback ad = new DelegateCallback(ACallback);
DelegateCallback bd = new DelegateCallback(BCallback);
DelegateCallback cd = new DelegateCallback(ACallback);
Console.WriteLine("static {0}  == {1}  {2} ", ad.Method, bd.Method, ad == bd);
Console.WriteLine("static {0}  == {1}  {2} ", ad.Method, ad.Method, ad == ad);
Console.WriteLine("static {0}  == {1}  {2} ", ad.Method, cd.Method, ad == cd);

Listing 14.10 shows a simple comparison of the Method properties. Assuming that ACallback and BCallback are both static methods, the first Console.WriteLine will print that the equality comparison is false, and the second will print true. This is as expected (they are either the same or they are different). The third case shows that delegate comparison is a value comparison and not just a reference comparison. Notice that even though ad and cd are different objects, comparing them for equality results in true. This is because both of the methods refer to ACallback. Listing 14.11 shows instance methods being compared.

Listing 14.11. Comparing Instance Methods
A a1 = new A();
B b1 = new B();
C c1 = new C();

ad = new DelegateCallback(a1.ACallback);
bd = new DelegateCallback(a1.BCallback);
Console.WriteLine("instance {0} .{1}  == {2} .{3}  {4} ", ad.Target, ad.Method, bd.Target,
 bd.Method, ad == bd);
cd = new DelegateCallback(a1.ACallback);
Console.WriteLine("instance {0} .{1}  == {2} .{3}  {4} ", ad.Target, ad.Method, cd.Target,
 cd.Method, ad == cd);

The first comparison returns false because the Target and Method properties are both different. The second comparison returns true because the Target and Method properties are the same even though the delegate objects are different. Listing 14.12 shows a comparison in which only the Target property is different.

Listing 14.12. Comparing Instance Methods
A a2 = new A();

bd = new DelegateCallback(a2.ACallback);
Console.WriteLine("instance {0} .{1}  == {2} .{3}  {4} ", ad.Target, ad.Method, bd.Target,
 bd.Method, ad == bd);

The comparison shown in Listing 14.12 will return false. Even though the Method properties are the same, the Target properties are different (defined on different instances of the same class). Listing 14.13 shows a comparison of two delegate chains.

Listing 14.13. Comparing delegate Chains
DelegateCallback dd = ad;
dd += bd;
dd += cd;

DelegateCallback ed = ad;
ed += bd;
ed += cd;
Console.WriteLine("chain {0}  == {1}  {2} ", dd.Method, ed.Method, dd == ed);

This comparison returns true because both chains contain the same type and number of delegates. Listing 14.14 shows what happens when the order is changed.

Listing 14.14. Comparing delegate Chains with Different Orders
ed = cd;
ed += bd;
ed += ad;
Console.WriteLine("chain {0}  == {1}  {2} ", dd.Method, ed.Method, dd == ed);

Notice that this comparison returns false. This is because even though the same delegates are used to build the chain, they are in a different order from the first delegate chain.

It is sometimes important to be able to compare delegates because of chains, as shown in Listing 14.14. You could set up a chain of delegates to notify various processes or computers of activities of which you want others to be aware. You could even set up a rudimentary “chat” that notifies others of your messages. In all of these scenarios, you will want to remove delegates from the chain, or you might want to see which delegates are set up. A comparison is needed to remove a particular delegate and to pick out a particular delegate from a chain.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.182.45