Using Code Snippets: The real deal

Translate This Page

Using Code Snippets: The real deal

Rate This
  • Comments 2

So your colleagues send out an email requesting information on how you manage code snippets and I thought that my colleagues were talking about code snippets.  Silly me, just like when someone asks me for a Word Template, I create and send a Word Template with the .DOTX extension.  Of course that isn’t what people are asking for, so like templates, my teammates were asking for how to manage snippets of code.


The real deal on code snippets is this:

You can easily make code snippets that can function automatically, like when you type in the word: “if” and then hit tab-tab in Visual Studio it will create an “if” statement:


Which is real handy, but what if you wanted to include a comment to surround that piece of code?


Here is how you do it:

For Code Snippets I usually use the snippet files and show the way to create snippets by copying an existing code snippet, discussing the way to create a “surround” with and where to place the code snippets.

I find that using Code Snippets is an easy way to reuse my comments and code for examples.  To show people how to use code snippets (with my audience I have to assume that they have never done any programming, so apologies if this seems somewhat simplistic).

This will work for Visual Basic and ASP.NET(code behind), but not C, CPP nor F#.

  1. Example process: Usually have my comments that I use to clarify my code inside of a snippet file that has the “Tab-Tab” functionality set.  It is easy to do, here is the XML that you could use to create code snippets:
    • First start with an existing code snippet, for instance I start with an if statement.
  2. Snippet code with will work when I type in Comments and then press tab-tab, this code snippet will also do a surround with the flower type of comments, which for my audience allows them to recognize that these are my comments and are for demo purposes only.
    • The surround doesn’t quite indent correctly initially, so if you know how to fix that let me know, but the “Tab-Tab” insertion works nicely
  3. This is easy to manage as you place your comments in the Visual C# Snippets under Visual Studio 2010, by default you place it here (for C#, VB has it’s own folder):
    • C:\Users\yournamehere\Documents\Visual Studio 2010\Code Snippets\Visual C#
  4. Here is the example code, I used the “if” snippet from the default installation path for snippets:
    • C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC#\Snippets\1033\Visual C#


Code Snippet
  1. <?xml version="1.0" encoding="utf-8" ?>
  2.   <CodeSnippetsxmlns="">
  3.   <CodeSnippet Format="1.0.0">
  4.     <Header>
  5.       <Title>Comments</Title>
  6.       <Shortcut>Comments</Shortcut>
  7.       <Description>o</Description>
  8.       <Author>Sam Stokes</Author>
  9.       <SnippetTypes>
  10.         <SnippetType>Expansion</SnippetType>
  11.         <SnippetType>SurroundsWith</SnippetType>
  12.       </SnippetTypes>
  13.     </Header>
  14.     <Snippet>
  15.       <Declarations>
  16.         <Literal>
  17.           <ID>expression</ID>
  18.           <ToolTip>Blah Blah Comment</ToolTip>
  19.           <Default>true</Default>
  20.         </Literal>
  21.       </Declarations>
  22.       <Code Language="csharp">
  23.         <![CDATA[
  24.             /*************Speaker Comments*****************
  25.             /* Comment Blah Blah Blah
  26.             /*********************************************/
  29.         $selected$ $end$
  30.     /**************End Speaker Comments *********************/]]>
  31.       </Code>
  32.     </Snippet>
  33.   </CodeSnippet>
  34. </CodeSnippets>
Leave a Comment
  • Please add 5 and 5 and type the answer here:
  • Post
  • Useful post! Thanx!

  • Thank you for your feedback, always appreciated.  Anyone who reads my blog needs an award. :)

Page 1 of 1 (2 items)
Generic Content

Legal Note:

  1. The author, Sam Stokes, is an employee of Microsoft
  2. There is no guarantee that anything I write is correct, I do try to make sure that what I write is correct.
  3. Use anything written in this blog at your own risk
  4. Test what I write about before using the information
  • Sometimes posts will disappear because I realized that they really don't fit with the total chaos of what I write about
  • Links are tested and available on date of publication, what others do with their links are out of my control


  • Whatever I write on this blog is unedited by management or anyone, which should be obvious from the randomness of the blog.  I simply write about things I like.
  • Microsoft does not restrict me in anyway, so I generallyblather about whatever I want to.
  • For personal reasons I will not write about:
    • Company confidential stuff, mainly because Microsoft is more transparent than any company I have ever worked for.
    • Other people unless they give me permission to write about them