Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Write Your Own Code Snippets

DZone's Guide to

How to Write Your Own Code Snippets

·
Free Resource

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 SnippetHow to Write Your Code Snippets

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.

 

Topics:

Published at DZone with permission of Gil Fink, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}