Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
142 views
in Technique[技术] by (71.8m points)

c# - 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.

question from:https://stackoverflow.com/questions/1307607/removing-event-handlers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...