Creating Our Own Snippets Using Visual Studio 2010






4.52/5 (16 votes)
Creating our own snippets in Visual Studio 2010 IDE
About This
In this blog, I am writing the basics of creating our own snippets in Visual Studio 2010 IDE.
Introduction
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#endif
directive.#region
- Creates a#region
directive and an#endregion
directive.~
- Creates a destructor for the containing class.attribute
- Creates a declaration for a class that derives fromAttribute
.checked
- Creates a checked block.class
- Creates a class declaration.ctor
- Creates a constructor for the containing class.cw
- Creates a call toWriteLine
.do
- Creates ado while
loop.else
- Creates anelse
block.enum
- Creates anenum
declaration.equals
- Creates a method declaration that overrides theEquals
method defined in theObject
class.exception
- Creates a declaration for a class that derives from an exception (Exception
by default).for
- Creates afor
loop.foreach
- Creates aforeach
loop.forr
- Creates afor
loop that decrements the loop variable after each iteration.if
- Creates anif
block.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 toSystem.Windows.Forms.MessageBox.Show
. You may need to add a reference to System.Windows.Forms.dll.namespace
- Creates anamespace
declaration.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 astatic int Main
method declaration.struct
- Creates astruct
declaration.svm
- Creates astatic void Main
method declaration.switch
- Creates aswitch
block.try
- Creates atry
-catch
block.tryf
- Creates atry
-finally
block.unchecked
- Creates an unchecked block.unsafe
- Creates anunsafe
block.using
- Creates ausing
directive.while
- Creates awhile
loop.
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.
Step 1
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.
Step 2
Modify the snippet name and properties as seen below. We can also make modifiable text in the code snippet. Here I have configured arg1
, 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
Step 1
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.
Step 2
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 SnippetAdd
.
Consuming Custom Snippets
Step 1
Type a word SnippetAdd
in the code editor window and presses TAB, it creates the code in the snippetAdd.snippet file.
Here arg1
and arg2
are editable variables and auto replace all other places it referred.
Conclusion
I hope this blog helps you to create the basics of creating our own snippets in Visual Studio 2010.