This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Visual Studio
VS 2005+

Creating Visual Studio Code Snippets

Visual Studio 2005 introduced the code snippet feature, which allows boilerplate code to be quickly inserted into a source code file. A number of standard snippets are provided. These may be enhanced with the creation of custom code snippets.

Registering a Code Snippet

Before you use a snippet in Visual Studio you must register it using the Code Snippets Manager. To begin, choose "Code Snippets Manager" from the Tools menu. If this menu option is not available, press Ctrl-K,B. The dialog box should be displayed.

Code Snippets Manager

To register the custom code snippet, click the Import button. You may then browse to the folder in which you placed the file, "IDisposable.snippet". Select the file and click the Open button to import it. A second dialog box is displayed listing the available snippet folders for the selected file. Choose "My Code Snippets" and click the Finish button to complete the import. You can check that the snippet is available by changing the language option to "Visual C#" and expanding the "My Code Snippets" branch in the TreeView. The dialog should be similar to that pictured above.

Inserting the Snippet

We can test the new code snippet by creating a C# project and inserting a class file. This snippet is designed to be inserted within the code block of a class so ensure that the text cursor is within this area. Type "idisp" and press the tab key twice to insert the code. If the Intellisense list appears you should see the snippet, which will have the title and description from the XML file.

21 February 2011