Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Creating Our Own Snippets Using Visual Studio 2010

4.52/5 (16 votes)
15 Jul 2011CPOL3 min read 61.4K  
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)