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:

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 <tab><tab>

Hopefully you won’t be too shocked to see the following, with the cursor highlighting the text “true”:

You can now replace anything you presser 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 <tab><tab>
  • 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 <enter>

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.

twittergoogle_plusredditpinteresttumblrmail

Written by Stephen Moon
email: stephen at logicalmoon.com
www: https://www.logicalmoon.com


Leave a Reply

Your email address will not be published. Required fields are marked *