Singleton snippet (PC/Mac) #XamarinMonth

Code Snippets (or templates in the Visual Studio for Mac world) are ready-made snippets of code you can quickly insert into your code. Here is my snippet/template to implement a singleton.

Singleton snippet (PC/Mac)  #XamarinMonth
Photo by Iker Urteaga / Unsplash

I'm surprised nobody wrote about this obvious snippet, and I'm also surprised it hasn't been added to Visual Studio out of the box, but I'm going to share with you my snippet/template for the Singleton design pattern.

Thanks to Luis Matos (@luismatosluna) for organising this Xamarin Month on the topic of Code Snippets! Check out the twitter hashtag to find more posts or see the monthly calendar of all posts here.

What are snippets/templates ?

Code Snippets (or templates in the Visual Studio for Mac world) are ready-made snippets of code you can quickly insert into your code. For example, the for code snippet creates an empty for loop.

More about Snippets in the docs.

How to install

It's very simple to install, here are the options :

  • Recreate the snippets on your machine, feel free to copy paste and customise 😉
  • (PC) Save singleton.snippet into %USERPROFILE%\Documents\Visual Studio 2019\Code Snippets\Visual C#\My Code Snippets
  • (Mac) Save singleton.template.xml into ~/Library/VisualStudio/8.0/Snippets

How to use

While in a class, type "singleton" and double-tap on tab and .. Magic!

You'll get a class that looks like this :

namespace SnippetProject
{
    public class MySuperClass
    {
        #region Singleton

        private static MySuperClass _instance;

        public static MySuperClass Current => _instance ??= new MySuperClass();

        #endregion

        private MySuperClass()
        {
            // TODO : Delete other constructors
            // TODO : Make MySuperClass sealed
        }
    }
}

The code

https://gist.github.com/framinosona/96d569142d5cf6578d24f056db93ccf3

singleton.template.xml :

<?xml version="1.0" encoding="utf-8"?>
<CodeTemplates version="3.0">
  <CodeTemplate version="2.0">
    <Header>
      <_Group>C#</_Group>
      <Version />
      <MimeType>text/x-csharp</MimeType>
      <Shortcut>singleton</Shortcut>
      <_Description>Adds a singleton design pattern</_Description>
      <TemplateType>Unknown</TemplateType>
    </Header>
    <Variables>
      <Variable name="className1" isEditable="false">
        <Function>GetCurrentClassName()</Function>
      </Variable>
      <Variable name="className2" isEditable="false">
        <Function>GetCurrentClassName()</Function>
      </Variable>
      <Variable name="className3" isEditable="false">
        <Function>GetCurrentClassName()</Function>
      </Variable>
      <Variable name="className4" isEditable="false">
        <Function>GetCurrentClassName()</Function>
      </Variable>
      <Variable name="className5" isEditable="false">
        <Function>GetCurrentClassName()</Function>
      </Variable>
    </Variables>
    <Code><![CDATA[#region Singleton
        private static $className1$ _instance;
        public static $className2$ Current => _instance ??= new $className3$();
        #endregion
        private $className4$()
        {
            // TODO : Delete other constructors
            // TODO : Make $className5$ sealed
        }]]></Code>
  </CodeTemplate>
</CodeTemplates>

singleton.snippet :

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Keywords>
        <Keyword>Singleton</Keyword>
      </Keywords>
      <Title>singleton</Title>
      <Author>Francois Raminosona</Author>
      <Description>Adds a singleton pattern to the current class</Description>
      <Shortcut>singleton</Shortcut>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
    </Header>
    <Snippet>
      <Code Language="CSharp">
        <![CDATA[#region Singleton

        private static $className$ _instance;

        public static $className$ Current => _instance ??= new $className$();

        #endregion

        private $className$()
        {
            // TODO : Delete other constructors
            // TODO : Make $className$ sealed
        }]]>
      </Code>
      <Declarations>
        <Literal Editable="false">
          <ID>className</ID>
          <Function>ClassName()</Function>
          <Default>className</Default>
        </Literal>
      </Declarations>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
For some reasons the Template for Visual Studio for mac didn't allow me to reuse the same variable (with the function GetCurrentClassName() ) twice. This is why I had to go dirty ... Let me know if you know a workaround.

Let me know in the comments or on Twitter if it helped you 🙂