Create a Custom Code Snippet In Visual Studio 2015

In this article, we will look into steps to be followed for creating and registering a custom code snippet for C# in Visual Studio 2015.  Code snippets are helpful for distributing and re-using code. It saves time in writing repetitive code. All we need is to create an XML file with all details for creating a custom code snippet.

Let's create the following XML code and name it AddClass-Snippet.snippet:

XML File
In the header section, we will set attributes like Title, Author etc.



In Reference section, we can add reference to assemblies and namespaces to be included. Here, I added a reference to System.ComponentModel.DataAnnotations.dll and imported same. So, when I use this snippet, it will include that assembly in my project and add using statement as well.

In Declarations section, we can declare literals or objects that need to be replaced within snippet and the same can be referred in the snippet using $name$. Here, I declared class name literal and object literal.

Under Code section, we can mention Language and add code within CData as shown below:



Basically, this snippet will insert a class and allows us to give a name for it and the same will be applied to the static variable as well.
 
Here's the complete code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">  
  3.   <CodeSnippet Format="1.0.0">  
  4.     <Header>  
  5.       <Title>Add Class</Title>  
  6.       <Author>Sateesh</Author>  
  7.       <Description>Adds boilerplate code for creating a class</Description>  
  8.       <Shortcut>addclass</Shortcut>  
  9.     </Header>  
  10.     <Snippet>  
  11.       <References>  
  12.         <Reference>  
  13.           <Assembly>System.ComponentModel.DataAnnotations.dll</Assembly>  
  14.         </Reference>  
  15.       </References>  
  16.       <Imports>  
  17.         <Import>  
  18.           <Namespace>System.ComponentModel.DataAnnotations</Namespace>  
  19.         </Import>  
  20.       </Imports>  
  21.       <Declarations>  
  22.         <Literal>  
  23.           <ID>classname</ID>  
  24.           <ToolTip>Replace with specified name</ToolTip>  
  25.           <Default>MyClass</Default>  
  26.         </Literal>  
  27.         <Object>  
  28.           <ID>Object</ID>  
  29.           <Type>System.Object</Type>  
  30.           <ToolTip>Replace with a object in your application.</ToolTip>  
  31.           <Default>myObject</Default>  
  32.         </Object>  
  33.       </Declarations>  
  34.       <Code Language="CSharp">  
  35.         <![CDATA[ 
  36. public class $classname${ 
  37.         private static $classname$ myObj= new $classname$(); 
  38. Object con = $Object$; 
  39.         public int Id 
  40.         { 
  41.             get; set; 
  42.         } 
  43.         [StringLength(20)] 
  44.         public string Name 
  45.         { 
  46.             get; set; 
  47.         } 
  48.         [EmailAddress] 
  49.         public string Email 
  50.         { 
  51.             get; set; 
  52.         } 
  53. } 
  54. ]]>  
  55.       </Code>  
  56.     </Snippet>  
  57.   </CodeSnippet>  
  58. </CodeSnippets>  
Let’s import into VS 2015 and use it, Open VS 2015 and click on Tools, Code Snippet Manager and click on Import and point to snippet file:



Select locations, under which snippet is to be saved. Click Finish and create a new console application and use our new code snippet by typing add class and pressing TAB:

code

This will add reference and import System.ComponentModel.DataAnnotations.dll into the project.

I am ending things here, I hope this article will be helpful for all.

Read more articles on Visual Studio:


Similar Articles