2

I saw some code using -= new EventHandler(anEvent) , can you one tell me what is the different to ‘ += new EventHandler’ ?

Thanks

4 Answers4

12

One adds a delegate to the collection of subscribers, the other removes it.

For example, if you had previously subscribed to an event, but you wanted the reference removed when you, say, closed a form, you would use the -= version and you would no longer be notified.

Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Maybe nice to mention that you are also allowed to just assign an event handler like `SomeEvent = new EventHandler...`. It will delete any existing subscription. It personally feel it's a short coming, it feels a bit like a hack. But maybe nice to make people aware that the construction is allowed too (also an easy mistake and ruin working software :)) – bas Jun 28 '22 at 10:32
2

The -= operator removes an even handler from an event, while += adds an event handler to an event.

For example:

if (checkSomething())
{
//handle clicks on myControl
myControl.Click += MyEventHanderMethod;
}
else
{
//stop handling clicks on myControl
myControl.Click -= MyEventHanderMethod;
}
nikmd23
  • 9,095
  • 4
  • 42
  • 57
2

I guess one should never use -= new EventHandler(anEvent) because the new event handler cannot yet in the event delegates list. One should do:

EventHandler eventHandler = new EventHandler(anEvent);
anObject.Event += eventHandler;
...
anObject.Event -= eventHandler;

Updated

Actually Ed is right, the delegate will check the target and method, not the handler object. Kinda a late for me to learn this, makes a lot of line I wrote obsolete...

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • And it does not address the question. – Ed S. Jul 06 '09 at 23:49
  • 2
    ...Though I will note that your way of thinking is more logical than the actual behavior. – Ed S. Jul 07 '09 at 00:00
  • Why would you ever have a "-= new" in real code? At best, it does nothing. – Steven Sudit Jul 07 '09 at 00:55
  • @Steven: That is completely wrong. It does exactly what we have said in our responses. On reason to do this; to avoid memory leaks. EventHandlers are references to an object. If the subscribed to event was external, that means there is still a lingering reference out there, i.e., it cannot be collected by the GC. There are many other reasons as well. You obviously don't understand some things about the language. – Ed S. Jul 07 '09 at 02:01
  • 1
    @Steven removing the handler is (most times) a *must*. Read http://rusanu.com/2009/01/19/clr-memory-leak . I just wasn't aware about the wrapper delegate comparison rules inside the delegate list, on which Ed is right. – Remus Rusanu Jul 07 '09 at 06:38
  • I'm all for removing the handler, as per the example above, particularly since you get memory leaks if you don't. I'm uncomfortable with "-= new" because it's depending on a behavior that we can't reasonably expect most C# programmers to understand. Instead, I would prefer "-= eventHandler", as in the example above. I'd accept "-= anEvent", but not "-= new". – Steven Sudit Jul 07 '09 at 09:43
2

Both operators are simply syntactical shortcuts for the internal framework methods System.MultiCastDelegate.Combine() and System.MultiCastDelegate.Remove(). Every delegate derives from System.MultiCastDelegate, which contains an internal private linked list of delegates. The new methods that += and -= are translated into by the IL compiler (Combine and Remove) effectively just add, (or remove, respectively) the internal delegates from the delegate argument (on the right hand side of the += or -+) to the internal link list of delegates on the left hand side,

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216