In this blog, I am writing the basics of creating our own snippets in Visual Studio 2010 IDE.
Basically, Visual Studio writes some set of code for us by typing shortcuts & pressing TAB.
List of Visual Studio snippet shortcuts:
#if - Creates an
#if directive and an
#region - Creates a
#region directive and an
~ - Creates a destructor for the containing class.
attribute - Creates a declaration for a class that derives from
checked - Creates a checked block.
class - Creates a class declaration.
ctor - Creates a constructor for the containing class.
cw - Creates a call to
do - Creates a
do while loop.
else - Creates an
enum - Creates an
equals - Creates a method declaration that overrides the
Equals method defined in the
exception - Creates a declaration for a class that derives from an exception (
Exception by default).
for - Creates a
foreach - Creates a
forr - Creates a
for loop that decrements the loop variable after each iteration.
if - Creates an
indexer - Creates an indexer declaration.
interface - Creates an interface declaration.
invoke - Creates a block that safely invokes an event.
iterator - Creates an iterator.
iterindex - Creates a "
named" iterator and indexer pair by using a nested class.
lock - Creates a lock block.
mbox - Creates a call to
System.Windows.Forms.MessageBox.Show. You may need to add a reference to System.Windows.Forms.dll.
namespace - Creates a
prop - Creates a property declaration and a backing field.
propg - Creates a property declaration with only a "
get" accessor and a backing field.
sim - Creates a
static int Main method declaration.
struct - Creates a
svm - Creates a
static void Main method declaration.
switch - Creates a
try - Creates a
tryf - Creates a
unchecked - Creates an unchecked block.
unsafe - Creates an
using - Creates a
while - Creates a
Apart from these default snippets, we can create our own code snippets. Generally, we can write custom snippets for repeatable codes across projects.
Creating Custom Snippets
Basically custom snippets save in .snippet file, it is XML based file, and we can manually create this XML file in a snippet acceptable format and add it into Visual Studio IDE. It is very difficult to write in XML.
Open source Snippet Designer available in CodePlex site. It can be downloadable and installed in Visual Studio 2010 IDE. Please see the reference section for download Snippet Designer.
Select a set of code wants to create snippets and right client, select Export as Snippet from the popup menu.
It will create a new .snippet file with the selected content.
Modify the snippet name and properties as seen below. We can also make modifiable text in the code snippet. Here I have configured
arg2 as replacement text.
Save this file in the local path with folder name Snippet Demo and named as SnippetAdd.snippet.
Adding Custom Snippet to VS 2010 IDE
Open VS 2010 IDE and select tools menu and click Code Snippets Manager item. Code snippet manager appears only if you install Snippet Designer tool.
It will open the Code Snippet Manager window.
Click add button and select the folder Snippet Demo, contains SnippetAdd.snippet file.
It will create the folder name Snippet Demo and create a snippet as
Consuming Custom Snippets
Type a word
SnippetAdd in the code editor window and presses TAB, it creates the code in the snippetAdd.snippet file.
arg2 are editable variables and auto replace all other places it referred.
I hope this blog helps you to create the basics of creating our own snippets in Visual Studio 2010.
Artha is a software developer in ASP.NET, C#.NET, VB.NET and ASP.
Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564)
Microsoft Certified Technology Specialist (MCTS) for Microsoft .NET Framework 3.5, ASP.NET Application Development (070-536, 070-562)
Microsoft Certified Professional (MCP) for Developing and Implementing Web Applications with Microsoft® Visual C#™ .NET and Microsoft® Visual Studio® .NET (070 315)
WIPO certification for General Course on Intellectual Property (DL-101)