Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Visual Studio auto-create an inline, local anonymous method for an event instead of creating concrete method?

When you use the += operator to create an event handler, Visual Studio gives you the option to press TAB to auto-create the handler. Doing so creates a concrete method like so:

consoleListBox.OnKeyPress += ConsoleListBox_OnKeyPress;

private static void ConsoleListBox_OnKeyPress(ConsoleControl sender, ConsoleKeyInfo keyInfo)
{

}

But is it possible to have VS create a local, inline anomyous handler like this?

consoleListBox.OnKeyPress += (sender, keyInfo) =>
{

};

It's so much cleaner, especially if I don't need the handler outside of the current context. I create these manually for now, but it would be nice if there were a way to auto-create local handlers.

like image 392
oscilatingcretin Avatar asked Sep 13 '19 19:09

oscilatingcretin


1 Answers

I am pretty sure Visual studio doesn't have an auto-complete for that. However, you can add your own code snippets which allow for some pretty cool things. I'd suggest you go look at Microsofts tutorial on how to do that here .

You can add snippets for litterally every piece of code you would like. The page gives an example of an auto square root piece of code which you can then add by typing "sqrt" and pressing TAB. You can add variables in the code as well which then allows you to enter your own value ones you use the snippet.

In your case you could make a snippet called "inlineevent" or just "inevent" or something like that.

Good luck and have fun!

like image 52
Rafaeltab Avatar answered Nov 13 '22 05:11

Rafaeltab