this article I will tell you how to create your own snippets using Visual Studio 2010.
you have your own code and it is used in many projects and used in each and
every file. Instead of copying and pasting this code you can create your own
snippets in Visual Studio 2010. That will save you time by avoiding to having to rewrite blocks of
For example, in MVVM, you have to implement the
iNotifyPropertyChanged notifier and this method is used in many properties.
Using the code
Part 1: Creating a Snippet File
There are three types of snippets available:
- Surrounds With
In this article I am creating expansion type of
snippet for Property block with MVVM functionality that is
code snippets are created in an XML file.
Step 1: First create an XML file in Visual Studio 2010 and save file as
The code snippet file starts with a
CodeSnippets element which has
xmlns attribute for
Step 2: Add the CodeSnippet section
Step 3: Add Header section within the Code Snippet section.
<Description> This Code snippet for property with Raisepropertychanged event </Description>
This section has elements like Title, shortcut for IntelliSense and Description.
- Title: Title for Snippet
- Shortcut: when you type using Intelligence it will shows list of snippet
including which you have created with given shortcut.
- Description: Detailed description of snippet on Mouse Over
of you snippet.
Author Name of snippet.
- Snippet Type: which type of snippet are
you going to create like Expansion (prop) or Surround with (try/catch).
CreateDeclaration section and then add Literals or Objects within Declaration Section.
Literal elements are editable values that are inserted by user into snippet. In the below code I have created three Literals. The first Literal describes the Type of
the property or variable, I have set the default value to int (you can give any datatype including (double, bool ,string etc.). The second Literal element is for
the editable name of the property. The third Literal element has a field, the user can give any variable name that is used within the property Get/Set.
<ToolTip>The variable baking this property</ToolTip>
Step 5: Code section describes the code for that snippets. In this snippet is added language is C#. Inside the
Code element, add the C# code for the snippet.
Step 6: Save snippet file as MyCodeSnippet.snippet
I added the MyCodeSnippet.snippet file in the download source. You can download this.
Part 2: Visual Studio Setting
Step 1: Open Visual Studio and click on Tool --> Code Snippets Manager item.
Step 2: It will show code snippet manager dialog box
Step 3: Select location for snippets and click on Finish button, your code snippet imported successfully.
Now you can use your snippet in development.
Step 4: Then press OK to save the snippet in VS 2010.
Create class file in your project. In that file start typing with your propmvvm, it will display list of IntelliSense code snippets including your snippetType propmvvm.
You will find the following
Then MVVM property snippet is added in your project successfully.
In this way you can create your own snippets and you can add to your Visual Studio 2010.
Don’t forget to leave your feedback and comments below! If you have any query,
mail me to Sujeet.email@example.com.