How to Write Your Own Code Snippets
IntelliSense 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:
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:
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:
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:
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
- From the Code Snippet Manager choose your language of choice and
press the Import button.
your snippet. This will open the Import Code Snippet dialog.
In the Import Code Snippet dialog choose where to put the snippet
and you are done.
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: