1

I had a typo in my code and added a delegate where an Event should be used:

public Action AfterItemSelectionCallback;

The class that's using the supposed event contains:

this.folderListViewModel.AfterItemSelectionCallback += this.HandleItemSelection;

and later on removes the handler:

this.folderListViewModel.AfterItemSelectionCallback -= this.HandleItemSelection;

This builds and runs fine without errors or warnings.

Enter Jetbrains Resharper 8.1. I installed a trial of Resharper and it complains that

"Delegate subtraction has unpredictable result."

  • Why would += be okay if -= is not?
  • Why does the compiler not complain if this is actually a problem?
Krumelur
  • 32,180
  • 27
  • 124
  • 263
  • I don't know why Resharper complains, but msdn tells me that `-=` **is** the way to *unsubscribe* an event: http://msdn.microsoft.com/en-us/library/ms366768.aspx – MrPaulch Feb 24 '14 at 15:20
  • Yeah, but I do _not_ have an event here. – Krumelur Feb 24 '14 at 15:21
  • Yeah, but events fundamentally *are* delegates that are invoked by the raising class. – MrPaulch Feb 24 '14 at 15:23
  • The other question really seems to cover it, although it is dealing with events. Looks like Resharper is always issuing this warning. even for "real" events. – Krumelur Feb 24 '14 at 15:24
  • @MrPaulch I know but I may case I can change the code to `= null` instead of using `-=` and then Resharper is quiet. So my assumption was, it is throwing that warning only for delegates but it also does it for events. – Krumelur Feb 24 '14 at 15:25

0 Answers0