27

When you subscribe to an event in code, Visual Studio automatically completes the code after += and generates the appropriate event handler:

button.Click += new EventHandler(button_Click);
//              ↑_____auto generated code_____↑

Notice how it explicitly creates the delegate instance: even though method groups are implicitly convertible to delegates since C# 2, the IDE behavior still hasn't changed in VS2010.

So I'd like to know, is there a way to generate code like this instead?

button.Click += button_Click;

EDIT
just to make things clear to everyone: the code above is not in a designer file (I wouldn't care which syntax is used if it was the case). It's the snippet that is triggered when you type += after an event name and press TAB


EDIT2
I reported this as a suggestion on Connect, you can vote for it if you also want the current behavior to be changed

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • 6
    Why do you assume this code is in a designer file? Don't you ever manually subscribe to an event? I don't care about code that is in designer files, but I want *my* code to be as readable as possible. The implicit conversion from method group to delegate seems much more readable to me, the explicit delegate instantiation just adds noise. – Thomas Levesque Dec 17 '10 at 20:07
  • (slightly ot) If you `button.Click -= button_Click` does that work the same way? – Greg Dec 17 '10 at 20:09
  • @Greg, not it doesn't... It would be nice, but I can live with it ;) – Thomas Levesque Dec 17 '10 at 20:11
  • 1
    Sorry, I thought you were talking about hooking events through the UI. I have just always deleted the new WhateverEventHandler manually, never really thought about changing it. – Ed S. Dec 17 '10 at 22:36
  • @Ed, yes, that's what I do too (Resharper makes it easy, just type Alt+Enter). It's just annoying that VS continues to call the delegate constructor even though it's been unnecessary for years... – Thomas Levesque Dec 17 '10 at 23:23
  • I really dont't see any reason for customizing this. What studio provides after you click tab (once or twice) is a good starting point to implement your handler the way you like. –  Dec 18 '10 at 15:00
  • 1
    @bakopanos costas, did you actually read the question ? I don't care about what is in the handler, I just don't want the explicit call to the delegate constructor, since it's unnecessary and just adds noise – Thomas Levesque Dec 18 '10 at 20:06
  • 3
    If you agree with Thomas, please vote for this on the Connect link provided. – si618 Mar 21 '11 at 04:46

3 Answers3

2

The suggestion on Connect has been marked as fixed, so the new behavior should be included in the next public build of VS11.

EDIT: just checked, it is indeed included in the beta.

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
1

Thanks for clarifying your question. Unfortunately, there isn't a way to configure or customize the event hookup code that is generated by the C# language service. To make matters worse, the Generate Method Stub feature won't work on an event hookup either -- though it should, and that is already fixed for the next release of Visual Studio.

Sorry that there isn't a better story for this particular feature. :-(

Dustin Campbell
  • 9,747
  • 2
  • 31
  • 32
0

I use this extension for Visual Studio since a couple of years.

Samuel
  • 12,073
  • 5
  • 49
  • 71