Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it means in C# : using -= operator by events?

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

like image 711
masoud ramezani Avatar asked Mar 02 '10 14:03

masoud ramezani


People also ask

What does %= mean in C?

%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.

What does i += 1 mean in C?

i+=i means the i now adds its current value to its self so let's say i equals 10 using this += expression the value of i will now equal 20 because you just added 10 to its self. i+=1 does the same as i=i+1 there both incrementing the current value of i by 1. 3rd January 2020, 3:15 AM.

What does =+ mean in C #?

The += operator performs enhanced assignments. The value of the expression to the right of the operator is added to the value of the variable to the left of the operator, and the result replaces the value of the variable. For example …

What || means in C?

If either operand is equal to 0, the result is 0. If the first operand of a logical-AND operation is equal to 0, the second operand isn't evaluated. || The logical-OR operator performs an inclusive-OR operation on its operands. The result is 0 if both operands have 0 values.


4 Answers

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.
like image 161
Jon Skeet Avatar answered Sep 19 '22 16:09

Jon Skeet


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 -=).

like image 30
T.J. Crowder Avatar answered Sep 21 '22 16:09

T.J. Crowder


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.

like image 21
Dan Tao Avatar answered Sep 23 '22 16:09

Dan Tao


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

like image 29
Aurril Avatar answered Sep 21 '22 16:09

Aurril