Code snippet is an amazing way to reuse code using templates that produce a base code for common statements like loops and conditionals.
Visual Studio has a Code Snippet Manager where we can perform an administration of the default code snippets included by default.
Depending on the workloads installed in Visual Studio we can see different code snippets per programming language or technology.
Go to Tools -> Code Snippet Manager

To create a code snippet, we need to use a template where we will fill the general information for the code that we want to generate.
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title></Title>
<Author></Author>
<Description></Description>
<Shortcut></Shortcut>
</Header>
<Snippet>
<Code Language="">
<![CDATA[]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
We have the following properties:
- Title: Name or general information
- Author: Creator or author
- Description: Details about your code snippets does
- Shortcut: Shortcut to call the code snippet when you are typing
Let’s create our first code snippet using the following information,
<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>If Windows condition</Title>
<Author>John Doe</Author>
<Description>Conditional to now if the operative system is Windows</Description>
<Shortcut>ifwin</Shortcut>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[if (OperatingSystem.IsWindows())
{
}]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
The preview code snippet produces a condition to validate if the operative system is windows or not. You can type ifwin to call this piece of code and press “tab” twice to generate the code.
You need to save the XML structure as a file using the extension “.snippet”

To include this new code snippet in Visual Studio you have 2 options:
- Add: where you can add a folder including various code snippets
- Import: Add one or more code snippets to an existing folder
In this case, we will use import and the new code snippet will be added to My CodeSnippets folder. Click on "Finish"

You need to confirm the folder where you will import the code snippet and finally click on “OK” to save the changes.

Now you can use ifwin condition in any C# file to validate quickly if the application is running in windows or not.

Now you can create your own code snippets and improve your productivity coding with Visual Studio!
For more information about code snippets go here.

Aaron CarterPosted Nov 20, 2022, 9:01 AM
Any idea how I can stop it from appearing in separate snippet folders in IntelliSense from the standard snippets, or else put some custom snippets in that same place so when I hit the keyboard shortcut for Insert Snippet ... or Surround with ... it doesn't come up with two *separate* folders I have pick from? It's an annoyance and a slowdown, almost makes me want to delete my custom snippets from the menu and find another way to do it (maybe just put them all inside a personal VSIX package?) ... without custom snippets when I do Surround with or Insert it just shows me a nice, clean list of the built-in snippets like #if, #region, if, try, prop, etc ... I definitely can't add an extra step to choosing between two folders only then to be able to flip between snippets with the arrow keys ... see what I mean? It's a coding slow-down that has a slight but still negative and cumulative effect on my workflow ...
Alessandro Del SolePosted Jun 8, 2022, 8:50 AM
I have built an integrated extension for this, which also allows for packaging and sharing. Have a look! https://marketplace.visualstudio.com/items?itemName=AlessandroDelSole.CodeSnippetStudio2022
Saravanakumar SekaranPosted Apr 2, 2022, 2:33 AM
Wow very nice article !!!