Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between these 2 ways of removing an event handler?

Is there a difference here?

Button1.Click -= new EventHandler(Button1_Click);

and

Button1.Click -= Button1_Click;

The second method doesn't seem to work for me but I've seen it used when Google-ing 'how to remove an event handler'. Edit: Actually neither are working for me, even so should either work interchangeably?

Update:

The reason these didn't appear to work for me is because I had AutoPostBack=true on my controls. I wasn't setting breakpoints to see if the events were called, I just watched the browser to see if it refreshed (meaning a post-back).

like image 294
Dean Avatar asked Jul 30 '12 00:07

Dean


1 Answers

Those two pieces of code are the same. The second syntax (called "method group conversion") was a new feature added in C# 2.0.

like image 153
Mark Byers Avatar answered Sep 24 '22 06:09

Mark Byers