Inserting Code Snippets

Keyboard:  CTRL + K, CTRL + X
Menu:  Edit -> IntelliSense -> Insert Snippet
Command:  Edit.InsertSnippet
Versions:  2008,2010
Published:  4/20/2010
Code:  vstipEdit0051

 

Download the seriously cool Tip of the Day Extension to get the daily tips delivered to your Start Page!

 

Let's talk about snippets.  For some reason, even though these have been around for a while, people still don't have this baked into their brain.  So let's assume you are in the editor:

C#

image  

 

VB

image

 

 

And you want to crank out an "if" statement.  You can type it out but that would be the hard way out.  Why not just use a snippet?  There are several ways to do this:

  1. Just type "if" and press TAB twice: 

    C#
    The piece of paper indicates this is a snippet in C#.
    image 

    VB
    In VB you won't have the piece of paper but will have a note in the tooltip that indicates this is a snippet:
    image

  2. Press CTRL + K, CTRL + X or Right-Click and choose Insert Snippet or Go to Edit -> IntelliSense -> Insert Snippet on the Menu Bar:

    C#
    You will get this prompt to Insert Snippet.
    image 

    Go to "Visual C#" then press ENTER:
     image

    Then type "if" and press ENTER:
    image

    VB
    You will get the same prompt as you did in C# to start but the path is different.  Go to "Code Patterns [.]" then press ENTER:
    image 
    Go to "Conditionals and Loops" then press ENTER:
    image 

    Pick the "if" statement you want:
    image 

 

No matter which option you choose the result is essentially the same:

C#

image

 

VB

image

 

You can now type in your condition and then the rest of your logic.  Enjoy!