How to create and use custom code snippets in Visual Studio?

Code snippets are the template codes which can be inserted into your code using shortcuts. You can add any of your code to code snippet manager and can be access it anytime when you need. In this article you will learn,
  • How to use the default code snippets
  • How to create and use custom code snippets?
Default code snippets
Default code snippets are the sample codes which comes with visual studio and these can be accessed using the keyboard shortcut: Cntrl + K and S
When you use the shortcut Cntrl + K and S, you will see the list of default code snippet names as shown in above picture.

In order to insert the code snippet, just type the name of code snippet and press TAB key double time. For example, if you want to insert the ‘for’ loop code, just type for and press TAB key twice to insert complete for loop code structure as shown below.
In the same way you can insert the other available code snippets like if, else, while, for, foreach, enum, do, class, property, constructor, etc...

Custom code snippets
Visual studio allows us to create custom code snippets and use them when we need. Custom code snippets are the XML files with .snippet extension.

Here I am creating an Employee class with few properties as a code snippet and the same was defined in XML file as shown below.

xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="">
  <CodeSnippet Format="1.0.0">
      <Description>Code snippet to define employee class with default properties and constructor.</Description>
      <Author>Srinivasa Rao Dhulipalla</Author>
          <ToolTip>name of the class</ToolTip>
      <Code Language="csharp">
            /// This class was generated automatically from code snippet

            public $type$ $className$
                public int Id { get; set; } 
                public string Name { get; set; } 
                public string Location { get; set; } 
                public $className$(int id, string name, string location)
                    this.Id = id;
                    this.Name = name;
                    this.Location = location;
  • Root node – CodeSnippet
    • Header
      • In the header of the code snippet you need to define the name, description and shortcut of the code snippet. So, you might notice that I am accessing the Employee code snippet using the shortcut ‘emp’
    • Snippet
      • Declaration – You can define the set of Laterals here, where each lateral is about the each placeholder mentioned in the ‘Code’ element
      • Code - Write the code in the specified language format which is mentioned in attribute. The code might have the placeholders in the format of $placeholder$ and the replacements for the same can be using the Lateral nodes
Once you are done with snippet XML template preparation, you can configure it using Code Snippet Manager
Go to VS -> Tools -> Code Snippet Manager
VS launches the Code Snippets Manager dialog
From this dialog either you can use Add or Import features,
  • Add: If you want to separate your code snippets to a separate folder instead using the existing folder, just go to your file system, create a folder, place .snippet file into this folder and finally add it here.
  • Import: If you want to just import the .snippet file to one of the existing folders, use Import feature.
Here I am going to use the ‘Add…’ feature, where I will add Employee code snippet to a separate folder. For this, I have created a folder Dotnet4Techies on Desktop and placed the .snippet file within this folder.

Click on Add… button -> navigate to Dotnet4Techies folder -> choose Select Folder
Now you can see our Employee code snippet with the metadata of it as shown below
Now your code snippet is ready to use. Go to your code file, type emp (is a shortcut) and press TAB key double time. By this time, you notice that Employee code got inserted.

That’s it! I hope now you get to know how to create and use the custom code snippets in Visual Studio.
Thank you for reading..!

No comments:

Powered by Blogger.