by Wojciech Sura

Custom code snippets in Visual Studio

Visual Studio’s editor provides quite useful feature: code snippets. Code snippets are pieces of frequently used code with – optionally – a few blank fields to fill in.

It turns out, that one may create his own code snippets and import them into Visual Studio. I’ll show you, how can you do it.

First of all, create a new file and name it, for instance, SerializedClass.snippet . Then open it in your favorite text editor and let’s start writing.

[xml]<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="">
<CodeSnippet Format="1.0.0">
<Title>XML serializable class</Title>
<Description>Creates an XML serializable class with constructor.</Description>

So far, everything should be self-explanatory. Now, before entering snippet’s source code, we may define some literals, which will be later replaced by values entered by user.

[xml] <Snippet>
<ToolTip>Name of the class</ToolTip>

ID specifies name, which will be used in snippet’s code. To inform, that piece of snippet is a literal, you have to surround it with dolar-signs: $ClassName$.

[xml] <Code Language="CSharp">
<![CDATA[ [XmlRoot("$ClassName$")]
public class $ClassName$
public $ClassName$()


Ok, now we have to import the snippet to Visual Studio. Start the IDE and choose Tools | Code Snippets Manager…. Then use the Import button to import new snippet to the IDE. The snippet will be copied to %USERPROFILE%\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets (assuming, that you use Visual Studio 2013, of course).


When the snippet is imported, it immediately is available. Simply write “xclass”:


And then press Tab key twice. Voila!


Read more about custom code snippets in the MSDN library.