Create Custom Code Snippet In Visual Studio 2015: Part Two

Before reading this article, please go through the following article:

In previous article, we had a look into the creation of custom code snippet for C# in Visual Studio 2015. Here, we will look into it more and understand functions available for C# code snippet and how to distribute the same for others. We can use the following functions within the code snippet.

  1. GenerateSwitchCases(EnumerationLiteral):

    This will generate a Switch statement and set of case statements as well based on the Enum passed to it. Let’s understand it by writing the following code:


    We mentioned Snippet title, shortcut, and used function GenerateSwitchCases, which accepts an Enum. Save it as myswitch.snippet and import same using Tools, Code Snippets Manager, then click Import.

    Let’s use our snippet by creating a sample console application and writing enumswitch and pressing TAB then entering enum name, we will add the following code with Switch and case statements,


  2. ClassName(): It returns class name that contains inserted snippet.

  3. SimpleTypeName(TypeName):

    Reduces TypeName to simplest form in the context in which the snippet was invoked. Let’s understand it with the below snippet.

    This snippet will add destructor by getting it's name from class, in whichthe   snippet is inserted. We are using SimpleTypeName to return Parallel.Foreach statement.

    After importing it, open .cs file and enter destructor, then press TAB, this will add a destructor to the class with Parallel loop in it. Similarly, using SimpleTypeName (global::System.Console) will return Console [simplest form].


    We can distribute this snippet file to others and they can use it by using Import Code Snippet option in VS. But, if we have multiple snippets for distribution, then we can use VSIX project for the same and include all snippets in it as Visual Studio extension. We need to install Visual Studio SDK for creating VS extensions.

    Let’s create a VSIX project in VS 2015 and name it mysnippets as shown below,


    Add folder snippets and include all snippets then set Build Action to Content, Copy to Output Directory to Copy always, and Include in VSIX to true under properties for each snippet as shown below,


    Add snippet.pkgdef under snippets folder for adding necessary registry entries and set its properties to Build Action to Content, Copy to Output Directory is to Copy always, and Include in VSIX is True with the following code in it,

    1. [$RootKey$\Languages\CodeExpansions\CSharp\Paths]   
    2. "snippets"="$PackageFolder$" 

    Now add pkgdef as an asset by going to properties of source.extension.vsixmanifest file under Assets -> New as shown below,


    We can test it in VS expermential instance by pressing F5 and going to Tools -> Code snippets Manager and language as CSharp. It will show our snippet added.

    For distributing our snippets, we can share our filemysnippets.VSIX saved in bin\Debug folder.


    I am ending things here, I hope this article will be helpful for all.
Read more articles on Visual Studio: