Click here to Skip to main content
15,880,503 members
Articles / Programming Languages / C#

Creating Our Own Snippets Using Visual Studio 2010

Rate me:
Please Sign up or sign in to vote.
4.52/5 (16 votes)
15 Jul 2011CPOL3 min read 60.3K   28   10
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:

  1. #if - Creates an #if directive and an #endif directive.
  2. #region - Creates a #region directive and an #endregion directive.
  3. ~ - Creates a destructor for the containing class.
  4. attribute - Creates a declaration for a class that derives from Attribute.
  5. checked - Creates a checked block.
  6. class - Creates a class declaration.
  7. ctor - Creates a constructor for the containing class.
  8. cw - Creates a call to WriteLine.
  9. do - Creates a do while loop.
  10. else - Creates an else block.
  11. enum - Creates an enum declaration.
  12. equals - Creates a method declaration that overrides the Equals method defined in the Object class.
  13. exception - Creates a declaration for a class that derives from an exception (Exception by default).
  14. for - Creates a for loop.
  15. foreach - Creates a foreach loop.
  16. forr - Creates a for loop that decrements the loop variable after each iteration.
  17. if - Creates an if block.
  18. indexer - Creates an indexer declaration.
  19. interface - Creates an interface declaration.
  20. invoke - Creates a block that safely invokes an event.
  21. iterator - Creates an iterator.
  22. iterindex - Creates a "named" iterator and indexer pair by using a nested class.
  23. lock - Creates a lock block.
  24. mbox - Creates a call to System.Windows.Forms.MessageBox.Show. You may need to add a reference to System.Windows.Forms.dll.
  25. namespace - Creates a namespace declaration.
  26. prop - Creates a property declaration and a backing field.
  27. propg - Creates a property declaration with only a "get" accessor and a backing field.
  28. sim - Creates a static int Main method declaration.
  29. struct - Creates a struct declaration.
  30. svm - Creates a static void Main method declaration.
  31. switch - Creates a switch block.
  32. try - Creates a try-catch block.
  33. tryf - Creates a try-finally block.
  34. unchecked - Creates an unchecked block.
  35. unsafe - Creates an unsafe block.
  36. using - Creates a using directive.
  37. while - Creates a while 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.

References

  1. http://msdn.microsoft.com/en-us/library/ms165392(VS.80).aspx
  2. http://snippetdesigner.codeplex.com/releases/view/50044

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead
India India
Artha is a Technical Lead in Windows Phone, WPF [MVVM, PRISM], ASP.NET [MVC 3.0 & 4.0], C#.NET, VB.NET and ASP.

Windows Phone

Microsoft Developer Network


Published Tools in Visual Studio Galleries


Published Source Codes in CodePlex


Microsoft Virtual Academy Profile


Published Articles in Code Project


Microsoft Certified Professional Developer (MCPD) Microsoft ASP.NET Developer 3.5 (070-564, 070-536, 070-562, 070 315)

Comments and Discussions

 
GeneralMy vote of 5 Pin
Jacksquad15-Jan-13 19:37
Jacksquad15-Jan-13 19:37 
GeneralRe: My vote of 5 Pin
Arthanarieaswaran Shanmugaraj16-Jan-13 0:08
Arthanarieaswaran Shanmugaraj16-Jan-13 0:08 
GeneralMy vote of 5 Pin
Farhan Ghumra28-Dec-12 22:19
professionalFarhan Ghumra28-Dec-12 22:19 
GeneralRe: My vote of 5 Pin
Arthanarieaswaran Shanmugaraj8-Jan-13 22:43
Arthanarieaswaran Shanmugaraj8-Jan-13 22:43 
GeneralMy vote of 5 Pin
amrswalha6-Oct-12 21:47
amrswalha6-Oct-12 21:47 
GeneralRe: My vote of 5 Pin
Arthanarieaswaran Shanmugaraj7-Oct-12 17:57
Arthanarieaswaran Shanmugaraj7-Oct-12 17:57 
GeneralMy vote of 4. Pin
_Amy26-Jul-12 22:51
professional_Amy26-Jul-12 22:51 
GeneralRe: My vote of 4. Pin
Arthanarieaswaran Shanmugaraj7-Aug-12 18:00
Arthanarieaswaran Shanmugaraj7-Aug-12 18:00 
Thanks for your comment.

Artha
QuestionNo option for export as snippet Pin
Rupesh Kumar Tiwari25-Oct-11 9:20
Rupesh Kumar Tiwari25-Oct-11 9:20 
AnswerRe: No option for export as snippet Pin
Arthanarieaswaran Shanmugaraj20-Dec-11 17:07
Arthanarieaswaran Shanmugaraj20-Dec-11 17:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.