10

When must we use this operator by events? What is its usage?

masoud ramezani
  • 22,228
  • 29
  • 98
  • 151

5 Answers5

24

Just as += subscribes you a handler to the event, -= unsubscribes it.

Use it when you no longer want a particular handler to be called when the event is raised. You often only need to use it the component raising the event is logically longer lived than the handler of the event - if you don't unsubscribe, the "event raiser" effectively has a reference to the handler, so can keep it alive longer than you want.

As noted in comments:

  • -= will only remove a single handler; if there are multiple handlers subscribed (even using the exact same delegate) it will still only reduce the handler count by 1. The last instance of the specified handler is the one removed. (So if you previously had handlers A, B, A, C subscribed in that order, and removed A, you'd end up with A, B, C.)
  • -= doesn't cause an error if the specified handler is not subscribed to the delegate already; it just ignores the request. This is true even if the event has no handlers subscribed to it at the moment.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 6
    +1. It's also worth mentioning that failing to unsubscribe to an event is often the cause of a program running out of memory, as it prevents objects becoming eligible for GC. – RichardOD Mar 02 '10 at 14:06
  • 1
    Jon, I'm a little frightened by the fact we both started our answers with "Just as..." ;-) – T.J. Crowder Mar 02 '10 at 14:16
  • what is the VB.NET version of `-=`..I mean how to unsubscribe from an event..? – techBeginner Nov 05 '12 at 13:45
  • @dotNETbeginner: I don't know offhand, but I'm sure if you look at the VB-specific documentation for events on MSDN you'll get the answer. – Jon Skeet Nov 05 '12 at 14:06
  • It would also be helpful to mention the no-op scenario. That is, if the handler wasn't subscribed to begin with, using -= to "unsubscribe" it doesn't complain. I just tested this and was even able to use -= on a null event, without crashing--presumably because this -= is syntactic sugar...? – Jon Coombs Oct 07 '14 at 05:02
  • Also, I should mention that -= only removes ONE copy of the handler. If somehow it's been added multiple times (which would surely be a bug), you can't sweep that under the rug with a single -= call. – Jon Coombs Oct 07 '14 at 05:13
  • IK it is too late to respond to @dotNETbeginner's input but for future visitors: `RemoveHandler` statement can be : https://msdn.microsoft.com/en-us/library/3xz97kac.aspx – NeverHopeless Apr 14 '15 at 14:32
10

Just as you can add event handlers via +=, you can remove them via -=.

For instance:

mybutton.Click += new EventHandler(myhandler);

You can later remove it like this:

mybutton.Click -= new EventHandler(myhandler);

...because event handlers for the same method and instance are equivalent (so you don't need to retain a reference to the handler you used with += and use that one with -=).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • "Because event handlers for the same method and instance are equivalent [...]" is this mentioned anywhere in the official documentation? Thanks – Alberto May 26 '22 at 09:48
3

The += and -= operators can be used in C# to add/remove event handlers to/from one of an object's events:

// adds myMethod as an event handler to the myButton.Click event
myButton.Click += myMethod;

After the above code runs, the myMethod method will be called every time myButton is clicked.

// removes the handler
myButton.Click -= myMethod;

After the above code runs, clicking on myButton will no longer cause myMethod to be called.

Dan Tao
  • 125,917
  • 54
  • 300
  • 447
1

I suspect that the background logic of the += is to add the handler to a list/array of event handlers for the given event. When -= is used, it compares your right hand argument to the list of event handlers it is holding for this event and deletes it from the list. If you do multiple += for a given event, each handler will get called.

Stated differently: += means add a method to the list of methods to call when the event occurs. -= means remove the specified method from the list of methods to call.

If all are removed, the event will have no handlers and the event will be ignored.

JimMoore
  • 31
  • 2
1

You remove the Eventhandler Function. C# Tutorial, Events and Delegates

Aurril
  • 2,469
  • 2
  • 24
  • 38