Creating Your Own Code Snippet

There is a good reference on the Microsoft site where you can go in details how to create all fields for snippets. But, I would like to get started with the basic script that I have created while working with Task Library.

Here is how you can implement it.

Observe the code below where I have added some code (you can modify the code to whatever you want.). Save this file with the .snippet file extension.

As you can see below, basically, the code snippet which we want to have in the code goes in CDATA block. (I would suggest you play around the syntax to get the exact format of the code).

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <CodeSnippets xmlns="">  
  3.     <CodeSnippet Format="1.0.0">  
  4.         <Header>  
  5.             <Title>ntsk</Title>  
  6.             <Shortcut>ntsk</Shortcut>  
  7.             <Description>Code snippet for Task via Factory</Description>  
  8.             <Author>Tapan Patel</Author>  
  9.         </Header>  
  10.         <Snippet>  
  11.             <Code Language="csharp">  
  12. <![CDATA[Task.Factory.StartNew(()=>  
  13. {  
  15. }  
  16. );]]>  
  17. </Code> </Snippet>  
  18.     </CodeSnippet>  
  19. </CodeSnippets>  
Once you save that file on the hard disk, go to Visual Studio and navigate to Tool -> Code Snippet Manager.

Code Snippet Manager

Here, you can add a folder if you have stored the snippet at a dedicated folder. Otherwiae, you can just import the snippet file as I did.

Once you launch the Snippet Manager, you should select C Sharp as a language if working with C#. Also note that the location shown below is the one where you will find your existing code snippets.

Code Snippet Manager

At the bottom of the page, you should see an option for importing the .snippet file. Click on that.

Code Snippet Manager

After that, navigate to the snippet file location and select the snippet file. Upon selection, it will ask you to check the appropriate snippet folder where you want to integrate (basically, referencing for future uses) your imported snippet.


Note that by default, you have "My Code Snippets" folder selected, which means it is going to copy the snippet file to that folder. So, if you select Visual C# along with "My Code Snippets", it is going to create two copies of that snippet. And, when you will try to utilize the snippet, it will ask you to select one out of the two copies.

So, I won’t do that. Instead, I will only select Visual C#. Select Visual C# and click on Finish ->Click OK.

You are all set.

Navigate to any .cs file just to test out the snippet.

You should see something, as shown below.


Tap twice and you should be able to see that code inserted.


Please leave your comments if you have any question or are facing any difficulty while setting up the snippet.


Up Next
    Ebook Download
    View all
    View all