How to Write Your Own Code Snippets

January 17, 2009

How to Write Your Own Code Snippets

IntelliSense code snippets How to Write Your Code Snippets
are a very useful tool for
developers in order to write
code faster.In the post I’m
going to describe how to
write your own code snippet and to show an example of a code snippet that
I wrote which divides classes by regions in the style that I like.

What are Code Snippets?

IntelliSense code snippets are basically Xml files that have a strict schema
and have a .snippet file name extension. They are used by developers for
faster code writing and therefore can reduce the time it takes to code.
When you locate a code pattern that always repeat itself in some varieties
you should consider writing it in a code snippet in order to save time.
Code snippets are also used by lecturers in order to write faster code in
their sessions (using the IntelliSense) in order to reduce the amount
of technical coding that they write and reduce problems that may occur.

Writing Your Own Code Snippet

Building a code snippet is easy. All you have to do is to follow the
code snippet XML schema. The first thing to do is to create a new
Xml file with the .snippet file name extension. After you have that
file you need to follow the code snippet XML schema in order to create
your snippet. In the file the first thing to add is the CodeSnippets element
with a Xml namespace to direct to the snippets namespace like this:

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
 
  </CodeSnippet>
</CodeSnippets>

After you wrote the namespace it will help you to create the snippet

by showing errors of missing Xml elements that you need to provide.

Every snippet has to have a snippet header. The header provide the details for

the snippet such as the Title, the Author, the Description and the Shortcut

which is the code you write to invoke the snippet in the IntelliSense
An example for a header can look like:

<Header>
  <Title>
    Arrange Class Snippet
  </Title>
  <Author>Gil Fink</Author>
  <Description>The snippet helps to divide a class to its parts using regions</Description>
  <Shortcut>ArrangeClass</Shortcut>      
</Header>

The last element that we have to include in the Xml is the Snippet itself.

The main element of the Snippet element is the Code element which inside

of it you provide the snippet code. You have to indicate which language the

snippet belongs (C#, VB or more). The code of the snippet needs to be

located inside of a <![CDATA[ …Code ]]> element.

An example of a Snippet element can look like:

<Snippet>      
  <Code Language="CSharp">
    <![CDATA[#region Consts
              
             #endregion
             
             #region Members
             
             #endregion
             
             #region Properties
             
             #endregion
             
             #region Ctor
 
             #endregion
             
             #region Methods
             
             #region API Public Methods
                                                               
             #endregion
             
             #region Private Methods
             
             #endregion
             
             #endregion]]>
  </Code>
</Snippet>

The following example is a code snippet that I wrote to help me arrange

classes to regions in the way that I like. This is a full example for how to

write a code snippet:

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>
        Arrange Class Snippet
      </Title>
      <Author>Gil Fink</Author>
      <Description>The snippet helps to divide a class to its parts using regions</Description>
      <Shortcut>ArrangeClass</Shortcut>      
    </Header>    
    <Snippet>      
      <Code Language="CSharp">
        <![CDATA[#region Consts
                  
                 #endregion
                 
                 #region Members
                 
                 #endregion
                 
                 #region Properties
                 
                 #endregion
                 
                 #region Ctor
 
                 #endregion
                 
                 #region Methods
                 
                 #region API Public Methods
                                                                   
                 #endregion
                 
                 #region Private Methods
                 
                 #endregion
                 
                 #endregion]]>
      </Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

 

How to Import a Code Snippet into the Code Snippet Manager?

After writing a code snippet the next thing to do is to import it to the

Code Snippet Manager of Visual Studio. The following steps will help you

to do this thing:

  • In Visual Studio choose Tools menu and from it the Code Snippet

    Manager
    :

    Code Snippet Manager 

  • From the Code Snippet Manager choose your language of choice and

    press the Import button.

    Code Snippet Manager Open  
    A dialog will be opened and in it browse the file system and find

    your snippet. This will open the Import Code Snippet dialog.

    Import Code Snippet

    In the Import Code Snippet dialog choose where to put the snippet

    and you are done.

Summary

Lets sum up, in the post I introduced code snippets. I also showed

how to write your own code snippet and then how to import it to

Visual Studio 2008’s Code Snippet Manager. Code snippets can reduce

the amount of coding time by writing patterns of code into a code snippet

fragment and then use that fragment through the IntelliSense.

The following link is a good place to start when you want to get more

information about code snippets:

creating and using IntelliSense code snippets.

DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

4 comments

  1. KimJanuary 18, 2009 ב 14:16

    I would seriously look into either R# or CodeRush as a replacement for the built in snippets. Both increases development productivity significantly.

    There’s no need to create snippets by hand. http://www.codeplex.com/SnippetEditor

    This was originally the VB Snippet Editor, but I believe that it now supports C# as well.

    Reply
  2. Gil FinkJanuary 18, 2009 ב 15:13

    Hi Kim,
    Thanks for the comment. I agree that R# or CodeRush can boost development significantly but they aren’t freeware (I used R# in the past). I also know the SnippetEditor tool which is ver effective and I just wanted to show how you can do it by yourself without any tools.

    Reply
  3. KimJanuary 18, 2009 ב 16:37

    “but they aren’t freeware”
    That’s true, but IMHO, not in the regular sense. Not using them (or similar) adds a real cost in lost productivity. So by not bying them, whoever pays for development actually pays more. :-)

    Reply
  4. Gil FinkJanuary 18, 2009 ב 17:04

    Hi Kim,
    It is right. Using R# or CodeRush or other tools in their category gives a boost for development. I agree with what you wrote – “So by not bying them, whoever pays for development actually pays more.”. Not all companies buy tools like that casue sometimes they don’t have the knowledge or because they want to cut the expenses (which in the long run are higher without the tools). In my current project I tried to convince my managers to have R# or CodeRush but they didn’t listen. Therefore, currently I’m not using one of them and I’m doing stright things like my own snippets. I Guess sometimes the old fashion ways are needed with all the existing tools.

    Reply