When must we use this operator by events? What is its usage?
%= 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.
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.
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 …
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.
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.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 -=
).
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.
You remove the Eventhandler Function. C# Tutorial, Events and Delegates
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With