Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing event handlers

Is this:

Button.Click -= new EventHandler(Button_Click); 

the same as this:

Button.Click -= Button_Click; 

I ask because to me it seems that the former is removing a new reference to a method, and the latter one is removing a method itself. But then again, maybe the new EventHandler part is implicit in the += or -= overload in case the programmer doesn't explicitly assign it like that?

In case it is different how about

Button.Click -= new EventHandler(Button_Click); 

VS

Button.Click -= Button_Click; 

Thanks.

like image 624
Carlo Avatar asked Aug 20 '09 17:08

Carlo


People also ask

Can you remove event listeners?

Event listeners can also be removed by passing an AbortSignal to an addEventListener() and then later calling abort() on the controller owning the signal.

Should you remove event listeners?

TLDR; Always remove event listeners when you don't plan on using them any longer.

How do I remove event listener in react?

Add the event listener in the useEffect hook. Return a function from the useEffect hook. Use the removeEventListener method to remove the event listener when the component unmounts.


1 Answers

It is the same. The second is merely syntactic sugar for the first, and equality comparison is overloaded appropriately for delegate types:

Two delegates of the same type with the same targets, methods, and invocation lists are considered equal.

Source: MSDN, Delegate.Equality Operator

like image 179
Konrad Rudolph Avatar answered Sep 21 '22 06:09

Konrad Rudolph