Cool Visual Studio 2017 Tip #3 : Code Snippets
One thing that I think differentiates someone who uses Visual Studio a lot from someone that doesn’t is whether they are familiar with the key strokes and short-cuts available to speed up typing. One of my favourite features is something called snippets which allows you to use pre-canned chunks of code using very simple code words to trigger them. For example, if I type “_cw_” and press tab twice in a C# source file, it will add the following line into the code:
1 | Console.WriteLine(); |
With the cursor placed conveniently between the brackets.
What Snippets are Available?
Many! Let’s takea look at some:
- Press Control-K then Control-B or,
- Go to the Tools Menu > Code Snippets Manager…
Under the Language Combo Box, choose “_C Sharp_” since that’s a good place to start. All being well, you will see the following:
You can see that I have expanded the Visual C# tree item but if you click on one of the examples, on the right it will tell you about that particular snippet works. Let’s take a look at “_while_” - under Snippet Type it offers Expansions and Surrounds With but what does all that mean? In Visual Studio, find a place within a method and type the following:
- while
Hopefully you won’t be too shocked to see the following, with the cursor highlighting the text “true”:
1 | while (true) |
You can now replace anything you pressed with what was there (true). OK. Quite handy, but that doesn’t much look like it has surrounded anything. Try this now:
- Remove the while and enclosing tags.
- Type: cw
- Type: “_Hello World_”
- Select the code which appears (the Console bit).
- Go to the Edit Menu > Intellisense > Surrounds With… menu item (Or just use Control-K Control-S)
- Type: while
Pretty nice, right? As you can see in the tree list there are many, many more, so just experiment with a few and see which ones you will use most.
Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com