23rd Jan 2020

Custom Code Snippets in C#


Written By, Akhtarali Ansari


A developer’s mind always finds some shortcuts to complete their work in some smarter way. Here we come up with one such smart solution, i.e., Code Snippet.

So, let’s start with the Code snippets.

What is a Code Snippet?

A Code Snippet is a term used for a little portion of reusable source code. It helps the developer in avoiding the typing of repetitive code for the same context.

Code Snippet Body Parts:

Name: It’s just a name for your snippet
Prefix: It’s a small word that will trigger your snippet in the editor. It looks like a macro.
Body: The actual code that gets copied in the editor when a snippet triggers.
Description: Sample details shown as a tooltip of the code snippet.

Create a Code Snippet:

Creating a code snippet is very easy. Create an XML file, add required elements, write your code and then save your file as “SnippetName.snippet”.

Code Snippet Template:

Let’s start writing our custom Code Snippet. Here, we have taken an example of a simple POST API code structure.

  1. Open the Notepad and copy above sample code into it.
  2. Set the title, author, description, and shortcut elements for the snippet.You can see this title in the Visual Studio’s Code Snippets Manager.

  3. Mention the language you want to use in the Code element. Here, we’re doing it for C#.
  4. Add the snippet code in <![CDATA[]>.
  5. Add Declaration and Imports elements as per your requirements. Declarations are used to set the cursor when the code snippet is copied to the code editor. Imports are used for importing the required namespace.

    Check the following image:
  6. Now, save the file with the “.snippet” extension. Our XML file is now ready for use.

Import Code Snippet:

  1. Open Visual Studio. Go to Tools > Code Snippet Manager.
  2. Click on Import.
  3. Select the folder location in which you have stored the XML snippet file.
  4. Open Code Snippet Manager and select the code snippet folder. Notice that in the right pan, the description, shortcut and author names are populated.
    The snippet file that we created will be copied at the following path:
    C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC#\Snippets\1033\. Now, we can start using our imported code snippets. Write your Snippet Name Prefix and press Tab. The code will be copied to our code editor.

Sample Code Snippet.


  • Reduces the estimated time for code integration.
  • Reduces the number of mistakes.
  • Keeps the same format at all places, so the code looks much better and readable.
  • Increases productivity.
  • Gives rest to your hand and wrist. (They will thank you).


There are many other features that are helpful for coding. The code snippet is one of the most helpful features for every programming language. We can create custom code snippets as per our editor requirements. Write a bunch of code on just a few taps of fingertips, do clean and smart coding, keep the same format for the project.

Written By,

.Net developer at Yudiz Solutions Pvt. Ltd