Make Code Snippet In Visual Studio

What is a Code Snippet

A code snippet is a small chunk of reusable code which is used in development and inserted in the code file using the context menu or some combination of the hotkeys and a specified keyword.

If-else block tries to catch or auto implement it property, we add it through the context, as well as some combination of hotkeys or the keywords.

There are 2 types of snippets
  1. Expansion Snippet
    This is added at the particular insertion point. For instance, if we want to add the if block, we type if and press the tab so the if keyword is expanded to the if(true){ } block, then this is inserted by the keyword if and by using the context menu.


    Type if and press tab and you get the if block; if you type tryf we get the try { } finally {} ; and if we type class and press tab we get the class declaration

  2. Surrounded-With Snippet 
    It surrounds the specific chunk of code “like in array index print”, we surrounded it with the for loop, which surrounded the snippet as we type.

    Return x; select the return x and then right click. Click on the surrounded snippet and then pick the desired snippet.

Step 1

  • Open VS and go to Code Snippet Manager.

  • We get the dialogue, which lists the snippet, according to the frameworks.

  • Select the framework for which you want to make the snippet.

  • In my case, I want to make any custom snippet for C# language.

  • Select Visual C# to expand it, which contains all the snippets provided by the Visual Studio.

  • Copy the path; we want to open that snippet and after that, we make it our own snippet.

  • Go to file menu, click open and click the file.

  • Open the snippet that is the snippet of the if statement written in the XML and save with the .snippet. Hit Ctrl+A and Ctrl+C.

    1. Title - Title of the snippet which you want to give 
    2. Shortcut - Shortcut code which you type and then press the TABand  the snippet is inserted
    3. Description - Little bit of description about the Snippet
    4. Author - Author of the snippet
    5. SnippetType - Two Types of Snippet
    6. Code section - Here is the code which we treat as the Snippet we chose;  language like cSharp,VB etc.

Step 2

  1. SqlConnection Con=newSqlConnection("XYZ");  
  2. SqlCommand cmd=newSqlCommand("XYZ",Con);  
  3. cmd.Parameters.AddWithValue("XYZ", 2.ToString()); //atleast for 1 param  
  4. Con.Open();  
  5. cmd.ExecuteNonQuery();  
  6. Con.Close();  


  • Let’s say we have a code we use again and again in the Application {in practice that is wrong to use in the Application again and again} but here, we demonstrate just for the sake of  knowledge.

  • Go to file menu, click new and add the file.

  • Select the XML and click the open button.

  • Paste all the code which is copied by you to the if snippet, in my case:
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <CodeSnippets  xmlns="">  
    3.     <CodeSnippet Format="1.0.0">  
    4.         <Header>  
    5.             <Title>if</Title>  
    6.             <Shortcut>if</Shortcut>  
    7.             <Description>Code snippet for if statement</Description>  
    8.             <Author>Microsoft Corporation</Author>  
    9.             <SnippetTypes>  
    10.                 <SnippetType>Expansion</SnippetType>  
    11.                 <SnippetType>SurroundsWith</SnippetType>  
    12.             </SnippetTypes>  
    13.         </Header>  
    14.         <Snippet>  
    15.             <Declarations>  
    16.                 <Literal>  
    17.                     <ID>expression</ID>  
    18.                     <ToolTip>Expression to evaluate</ToolTip>  
    19.                     <Default>true</Default>  
    20.                 </Literal>  
    21.             </Declarations>  
    22.             <Code Language="csharp"><![CDATA[if ($expression$)  
    23.     {  
    24.         $selected$ $end$  
    25.     }]]>  
    26.             </Code>  
    27.         </Snippet>  
    28.     </CodeSnippet>  
    29. </CodeSnippets>  
  • Paste it to the XML file.

  • Hit Ctrl+S. Name it “SQL” , select the place on the desktop and then save with the extension of the .snippet.

  • Do some changes in the code and after that, paste the code in the code section of the XML file and after the changes will look like:

  • Hit Ctrl+S and exit the file.

  • Go to the snippet manager tools => snippet manager.

  • Select the SQL.snippet file form the desktop. It will ask the location but don’t changeit and make it default. 

All the work is done and it's the time to check the snippet.

Open any VS Project type “SQL”, press tab two times, and you will get the snippet.

Press the tab two times.


This is the complete tutorial to make the code snippet in Visual Studio. The code snippet rapidly enhances the productivity of the developer. I am enjoying the valuable feature of Visual Studio.

Up Next
    Ebook Download
    View all
    View all