To New(EventHandler) or !To New(EventHandler)

J

John Kraft

I recently installed Resharper to give it a try, and it has made me
wonder about a few things. The most curious of which concerns event
handlers. Every single example I've ever seen, every line of real
code I've ever seen, and every demo I've ever seen follows the same
patters.

SomeEvent += new EventHandler(EventHandlerMethod);

....

private void EventHandlerMethod(object sender, EventArgs e)
{
...
}


Resharper has been suggesting that the 'new EventHandler()' is
unnecessary code. When I tell it to do it's thing, it changes the
line of code to

SomeEvent += EventHandlerMethod;

I've since been using the latter form to keep Resharper happy, and
because it looks cleaner.

My questions are, is this acceptable? Is this going to cause some
kind of problem or strangeness that I'm not aware of? If there are no
problems with this form, why does Everyone use the new EventHandler
form?


Thanks,

John
 
J

Jon Skeet [C# MVP]

I recently installed Resharper to give it a try, and it has made me
wonder about a few things. The most curious of which concerns event
handlers. Every single example I've ever seen, every line of real
code I've ever seen, and every demo I've ever seen follows the same
patters.

Most of them were probably written with C# 1 in mind.
SomeEvent += new EventHandler(EventHandlerMethod);

...

private void EventHandlerMethod(object sender, EventArgs e)
{
...

}

Resharper has been suggesting that the 'new EventHandler()' is
unnecessary code. When I tell it to do it's thing, it changes the
line of code to

SomeEvent += EventHandlerMethod;

I've since been using the latter form to keep Resharper happy, and
because it looks cleaner.

The latter being a very good reason :)
My questions are, is this acceptable?

Positively encouraged, IMO.
Is this going to cause some kind of problem or strangeness that I'm not aware of?

It means that your code won't compile with a C# 1 compiler, but that's
about it. The generated code is the same.
If there are no problems with this form, why does Everyone use the new EventHandler
form?

Well, I wouldn't say that "everyone" does - but it's certainly true
that a lot of people aren't actually aware of the changes in delegate
creation syntax that C# 2 introduced.

Jon
 
B

Ben Voigt [C++ MVP]

If there are no problems with this form, why does Everyone use the
Well, I wouldn't say that "everyone" does - but it's certainly true
that a lot of people aren't actually aware of the changes in delegate
creation syntax that C# 2 introduced.

It's mainly the designers that use the long syntax. Intellisense also
auto-completes an event subscribe statement (+=) with the long syntax but I
always wipe out the unnecessary "new DelegateTypeName()" bits.
 
J

Jon Skeet [C# MVP]

It's mainly the designers that use the long syntax. Intellisense also
auto-completes an event subscribe statement (+=) with the long syntax but I
always wipe out the unnecessary "new DelegateTypeName()" bits.

Ah, that's true. I wonder how much effect Intellisense has on people's
coding style in general...

Jon
 
C

Chris Shepherd

John said:
My questions are, is this acceptable? Is this going to cause some
kind of problem or strangeness that I'm not aware of? If there are no
problems with this form, why does Everyone use the new EventHandler
form?

Definitely acceptable. I'd bet that everyone using the old form is a
combination of C#1 syntax, as well as tab completion in VS2k5, the
latter of which automatically inserts the "new EvendHandler()" bit for you.

Chris.
 
J

John Kraft

On Tue, 17 Jun 2008 09:56:35 -0500, John Kraft

Thank you for the info. I will happily continue to use the new
syntax.

Thanks,

John
 
I

Ignacio Machin ( .NET/ C# MVP )

Ah, that's true. I wonder how much effect Intellisense has on people's
coding style in general...

Jon

Hi,

A lot IMO, personally I always code the long version , mostly because
the Intellisense. What can be easier than two TABS? :)
 
J

Jon Skeet [C# MVP]

Ignacio Machin ( .NET/ C# MVP ) said:
A lot IMO, personally I always code the long version , mostly because
the Intellisense. What can be easier than two TABS? :)

Well, if you use Intellisense and want to create a new method, it fills
it in with a horrible name defying all conventions. I generally write
the method, then start typing the method name, then Ctrl-Space does the
rest. (IIRC)

I find it more readable in the short version - a difference well worth
an extra few keystrokes.
 
B

Ben Voigt [C++ MVP]

Jon said:
Well, if you use Intellisense and want to create a new method, it
fills it in with a horrible name defying all conventions. I generally
write the method, then start typing the method name, then Ctrl-Space
does the rest. (IIRC)

While I usually use Intellisense to generate a horribly-named stub method,
rename the method, fix the call.
I find it more readable in the short version - a difference well worth
an extra few keystrokes.

The argument list often takes more than a few keystrokes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top