Visual Studio Snippets… Very useful but not really widely discussed topic at all at workplaces. Recently one of my colleagues shared a snippet file to use but many of my other colleagues around me were not really very familiar with the snippet concept itself. So here I am with worth reading blog on Snippets.

Snippets are the code blocks which can be defined as templates in xml format with well-defined elements and attributes.   The example of most common snippet is “prop”/”propfull” in visual studio which many of us are using in day to day code writing. We just need to type “prop” and press Tab key twice and visual studio will generate generic code block for property in code behind file.

Create Snippet…

Now we will create a new snippet “propfpc” for Property but with some addition to default “prop” snippet.

  1. Copy following xml code to Notepad file.
  2. Save File with .snippet extension.
  3. Observe the file which is very well self explanatory with well defined Elements and Attributes.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="<a href="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet</a>">
<CodeSnippet Format="1.0.0">
<Header>
<Title>propfpc</Title>
<Shortcut>propfpc</Shortcut>
<Description>Code snippet for an automatically implemented property with property change call</Description>
<Author>Nikhil Thaker</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>type</ID>
<ToolTip>Property Type</ToolTip>
<Default>int</Default>
</Literal>
<Literal>
<ID>property</ID>
<ToolTip>Property Name</ToolTip>
<Default>MyProperty</Default>
</Literal>
<Literal>
<ID>_property</ID>
<ToolTip>_property Name</ToolTip>
<Default>_myProperty</Default>
</Literal>
</Declarations>
<Code Language="csharp"><![CDATA[
private $type$ $_property$;
public $type$ $property$
{
get { return $_property$;}
set { $_property$ = value; FirePropertyChanged("$property$");}
}
$end$]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>

Import Snippet to Visual Studio…

Go to Visual Studio –> Tools –> Code Snippets Manager –>Click “Import” button –> Select the snippet file you just created –> Select Location –> Click “Finish” button

Using Snippet in Visual Studio…

Now you are ready to use the snippet in Visual Studio.

  1. Open any code behind file in Visual Studio.
  2. Type shortcut you gave for snippet. In this case its “propfpc”.
  3. Press Tab key twice and bingo.. Your snippet has been implemented in file.

Here are some MSDN links for getting into more details on Visual Studio IntelliSense Snippets…

http://msdn.microsoft.com/en-us/library/ms165392(v=vs.80).aspx

http://msdn.microsoft.com/en-us/library/z4c5cc9b(v=vs.80).aspx

Here is the link for SnippetDesigner (Visual Studio Extension) on CodePlex which simplifies snippet creation…

http://snippetdesigner.codeplex.com/

Feel free to put comments, ask questions and start discussions on Visual Studio IntelliSense Snippets here.