13.07.2015 Views

C# in Depth

C# in Depth

C# in Depth

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Delegates39without worry<strong>in</strong>g about consistency, thread safety, or anyone try<strong>in</strong>g to change theiractions. This is just the same with delegate <strong>in</strong>stances as it is with str<strong>in</strong>gs, which arealso immutable. The reason for mention<strong>in</strong>g this is that Delegate.Comb<strong>in</strong>e is just likeStr<strong>in</strong>g.Concat—they both comb<strong>in</strong>e exist<strong>in</strong>g <strong>in</strong>stances together to form a new onewithout chang<strong>in</strong>g the orig<strong>in</strong>al objects at all. In the case of delegate <strong>in</strong>stances, theorig<strong>in</strong>al <strong>in</strong>vocation lists are concatenated together. Note that if you ever try to comb<strong>in</strong>enull with a delegate <strong>in</strong>stance, the null is treated as if it were a delegate<strong>in</strong>stance with an empty <strong>in</strong>vocation list.You’ll rarely see an explicit call to Delegate.x += y;x = x+y;x = Delegate.Comb<strong>in</strong>e(x, y);Figure 2.2 The transformation processused for the <strong>C#</strong> shorthand syntax forcomb<strong>in</strong><strong>in</strong>g delegate <strong>in</strong>stancesComb<strong>in</strong>e <strong>in</strong> <strong>C#</strong> code—usually the + and += operatorsare used. Figure 2.2 shows the translationprocess, where x and y are both variables of thesame (or compatible) delegate types. All of this isdone by the <strong>C#</strong> compiler.As you can see, it’s a straightforward transformation,but it does make the code a lot neater.Just as you can comb<strong>in</strong>e delegate <strong>in</strong>stances,you can remove one from another with theDelegate.Remove method, and <strong>C#</strong> uses the shorthandof the - and -= operators <strong>in</strong> the obvious way. Delegate.Remove(source, value)creates a new delegate whose <strong>in</strong>vocation list is the one from source, with the list fromvalue hav<strong>in</strong>g been removed. If the result would have an empty <strong>in</strong>vocation list, nullis returned.Table 2.1 shows some examples of the results of comb<strong>in</strong><strong>in</strong>g and remov<strong>in</strong>g delegate<strong>in</strong>stances. I’ve used the notation [a, b, c] to <strong>in</strong>dicate a delegate with an <strong>in</strong>vocationlist of actions a, b, and c (whatever they may happen to be).Table 2.1A selection of examples of comb<strong>in</strong><strong>in</strong>g and remov<strong>in</strong>g delegates, show<strong>in</strong>g what the resultis and whyOperation Result Notes[a] + [b] [a, b] --[a] + null [a] null counts as an empty <strong>in</strong>vocation list.null + [a] [a] --[a] + [b, c] [a, b, c] --[a, b] + [b, c] [a, b, b, c] Duplicates are allowed.[a, b, c] - [b] [a, c] The removal list doesn’t have to be at the end…[a, b, c, d, b, c] - [b, c] [a, b, c, d] … but the last occurrence of the removal list isremoved.[a] - [b] [a] No-op removal of nonexistent list.Licensed to Rhona Hadida

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!