Is there any use for explicitly using the "new EventHandler" declaration?

When assigning event handlers something like the MenuItem context, for example, there are two valid syntaxes:

 MenuItem item = new MenuItem("Open Image", btnOpenImage_Click); 

... and ...

 MenuItem item = new MenuItem("Open Image", new EventHandler(btnOpenImage_Click)); 

I also note that the same applies to this:

 listView.ItemClick += listView_ItemClick; 

... and ...

 listView.ItemClick += new ItemClickEventHandler(listView_ItemClick); 

Is there any special advantage for the second (explicit) compared to the first? Or is it a more stylistic question?

+8
c #
source share
4 answers

In C # 1.0, you had no choice but to explicitly define the delegate type and purpose.

With C # 2.0, the compiler allows you to express yourself in a more concise way by implicitly converting from a group of methods to a compatible delegate type. It really is just syntactic sugar.

Sometimes you have no choice but to use syntax with long branches if the correct overload cannot be solved from a group of methods due to ambiguity.

+9
source share

The syntax for the old version of the C # compiler (<= 1.1). Do not need anymore. Current compilers are complex enough to figure this out.

There is one (little) benefit, sometimes. If you assign "+ =" event handlers, the Intellisense autocomplete function can make the write code a little faster.

+9
source share

The time is only when it is useful - if it would be otherwise ambiguous - for example, if it was MenuItem(string, Delegate) - if there were several overloaded equally suitable ones that would correspond to the signature. It also includes var syntax (shown below) and type type inference (not shown):

 EventHandler handler = SomeMethod; // fine EventHandler handler = new EventHandler(SomeMethod); // fine var handler = new EventHandler(SomeMethod); // fine var handler = (EventHandler)SomeMethod; // fine var handler = SomeMethod; // not fine 

In all other cases, it is redundant and not needed in any compiler since version 2.0.

+7
source share

Associated with your editing. The imposition of handlers does not actually affect the use of new or not, but there is a slight difference in removing handlers like this

 listView.ItemClick -= listView_ItemClick; 

and

 listView.ItemClick -= new ItemClickEventHandler(listView_ItemClick); 

although it is unlikely to affect most scenarios. The first version, without the new keyword, is supposedly more efficient.

Detailed explanation in this post , but conclusion

So both work, but which one should we use? If events are signed / unsubscribed once at the beginning / end, as in a typical WinForm, then this does not really matter. However, if this is done several times, then the second approach is preferable, since it is less than expensive heap allocations and will work faster

(the second approach in this post is for those who don't have the new keyword)

Having said that this seems like micro-optimization for me, so this is unlikely to be a bottleneck in most cases.

+5
source share

All Articles