Delegate subtraction has unpredictable semantics

Skip to end of metadata
Go to start of metadata

The .Net framework defines operator overloads for the + and - operators on the Delegate data type. The + operator effectively combines delegates so that a multiple delegates are called within one. The - operator, on the other hand, attempts to remove the delegate invocation list from a delegate.

The list discinction is important here: what this implies is that subtraction of a delegate effectively implies a subtraction of lists. For example, in the code below, removing the A and C delegates from the ABC invocation list via the expression s - (a + c) has no effect whatsoever, because AC is not a sublist of ABC.

It’s also worth noting that sublist removal happens from the end of the list. For example:

Since the above mechanics can lead to unpredictable results, ReSharper issues a warning whenever it encounters a delegate subtraction operator.

Labels:
None
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.