79

Take the below code:

private void anEvent(object sender, EventArgs e) {
    //some code
}

What is the difference between the following ?

[object].[event] += anEvent;

//and

[object].[event] += new EventHandler(anEvent);

[UPDATE]

Apparently, there is no difference between the two...the former is just syntactic sugar of the latter.

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Andreas Grech
  • 105,982
  • 98
  • 297
  • 360
  • 4
    A tool like Resharper will recommend you remove the superfluous code since it just adds noise. – Chris Marisic Feb 15 '09 at 23:34
  • 1
    possible duplicate of [C# Event handlers](http://stackoverflow.com/questions/26877/c-sharp-event-handlers) – nawfal Jul 06 '14 at 20:21
  • The first line can only be understood by experienced C# programmers. The second line can be understood by both experienced C# programmers and those who are newer to the language. – CJ Dennis Nov 27 '17 at 22:23
  • @CJDennis - I see your point, but here is a possible counter-argument: it is better to use the paradigm that the newcomer will (almost always) encounter for events. Otherwise, they will be confused a different way: wondering why sometimes they see code saying "new EventHandler", and other times they don't. – ToolmakerSteve Jan 06 '20 at 20:05

4 Answers4

83

There is no difference. In your first example, the compiler will automatically infer the delegate you would like to instantiate. In the second example, you explicitly define the delegate.

Delegate inference was added in C# 2.0. So for C# 1.0 projects, second example was your only option. For 2.0 projects, the first example using inference is what I would prefer to use and see in the codebase - since it is more concise.

driis
  • 161,458
  • 45
  • 265
  • 341
18
[object].[event] += anEvent;

is just syntactic sugar for -

[object].[event] += new EventHandler(anEvent);
Martin Jonáš
  • 2,309
  • 15
  • 12
11

I don't think there is a difference. The compiler transforms the first into the second.

Megacan
  • 2,510
  • 3
  • 20
  • 31
1

For newcomers, I try to simply demystify how both usages are two sides of the same coin. When we look at its declaration we get the following by hovering our mouse pointer to + or =+ operators of EventHandler.

EventHandler EventHandler.operator +(EventHandler left, EventHandler right)

We are pretty sure from now on that it is a sort of operator overloading, which is +, along with not knowing its implementation detail. At this point, we can make use of ILSpy showing that the overloaded operator corresponds to the following method somehow.

(EventHandler)Delegate.Combine(< The EventHandler reference >, < The signature matched method implementation of the reference in question >);

For example,

myEventHandlerDelegateRef = (EventHandler) Delegate.Combine(
                    myEventHandlerDelegateRef,
                    new EventHandler(myEventHandlerRefDefinition)
                    );

As it can be clearly observed, the instantiation operator, new, is used internally.