Dean
Dean

Reputation: 4594

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

Upvotes: 8

Views: 111

Answers (2)

Greg9Strat
Greg9Strat

Reputation: 74

Functionally, no. There is no difference between the two. As to your issue about neither working, if this is an ASP.NET app, you may want to disable auto event wireup in the aspx page.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838276

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

Upvotes: 8

Related Questions