Wednesday, December 16, 2015

Visual Studio Shortcuts: Surround With

I really love snippets in Visual Studio. They save me a lot of typing. But there's a feature that we can use with code snippets that isn't very obvious but is extremely useful: Surround With.

And for those of you who think this is something new, it isn't. It's been around for a long time.

Try/Catch Snippet
The try/catch code snippet is one of my favorites. To use the standard snippet, we just type in "try":

If we look at the note, we see that we just have to press Tab twice to use this. And when we do, it stubs out the code for us:

Also note that the "Exception" is currently highlighted. This means that we can type in a new exception type without moving the cursor.

This is great when we are writing some new code. But usually, we already have a block of code that we want to wrap in a "try" block. So, we probably use the "try" snippet and then cut/paste the existing code to move it into the "try" block.

But there's an easier way: Surround With.

Surround With Try/Catch
Let's look at a shortcut. For this we have a method with some existing code:

We want to wrap the middle part of this method in a try/catch block.

Note: This code is taken from "I'll Get Back to You: Task, Await, and Asynchronous Methods". The source is on Github:

Instead of using the "try" snippet by itself, first, we'll highlight the block of code and then use the keyboard shortcut "Ctrl+K, Ctrl+S". This gives us a popup:

The popup has a pretty long list of code snippets that we can use. In this case, we'll type in "try" (just like we did earlier):

Then when we press "Tab" or "Enter", we see our code is surrounded with a "try" block:

Notice that the "Exception" is highlighted (just like with our original snippet). We can type in a new exception type:

And then when we press "Enter", the cursor moves to the inside of the "catch" block:

And then we just need to type in our own code:

This makes it really easy to add a try/catch block to our code. Let's try this again, but with a try/finally instead.

Surround With Try/Finally
Let's go back to our original method:

We'll take a slightly different approach. Instead of handling the exception here, we'll assume that we have an exception handler upstream that will take care of things. But we still have some code in this method that we have to worry about.

Notice that at the top of the method, we disable a button. Then at the bottom of the method, we re-enable it. We want to wrap this in a try/finally so that the button gets re-enabled even if there's a problem along the way.

So we'll highlight the same block that we had before, and use "tryf" for the try/finally snippet:

The output gives us an empty "finally" block:

Then we can move the code that re-enables the button into the "finally" block:

This way, we know that our button will get re-enabled, even if there is an exception along the way.

Other Snippets
When we use "Surround With", the snippets that come up all have a body of some sort.

For example, we can use the "if" snippet to wrap code in an "if" conditional. We can use the "while" snippet to wrap code in a "while" loop. The same is true for "for" and "foreach", and also for "using" and even creating regions ("#region").

Wrap Up
One of the reasons that "Surround With" isn't well known is because it's hidden pretty well. It's available on the right-click menu:

And it's also available with the keyboard shortcut (which I use all the time).

"Surround With" is one of those great little tools that makes Visual Studio such a great programming environment.

Happy Coding!