October 11, 2005

Singleton Implementation in c#

At http://csharpindepth.com/Articles/General/Singleton.aspx can be found a full description of the different c# singletons and their advantages and disadvantages
Here is the fast thread safe one:
#region Fast Thread Safe Singleton Implementation

static $ClassName$()
{}

private $ClassName$()
{}

private static readonly $ClassName$ instance= new $ClassName$();

public static $ClassName$ Instance
{
  get { return instance; }
}

#endregion
as a 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>SimpleSingleton</Keyword>
      </Keywords>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>SimpleSingleton</Title>
      <Author>R Bovill</Author>
      <Description>Make a threadsafe singleton class</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>SimpleSingleton</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>ClassName</ID>
          <ToolTip>
          </ToolTip>
          <Default>
          </Default>
          <Function>ClassName()</Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[
        #region Fast Thread Safe Singleton Implementation
        
        static $ClassName$()
        {}

        private $ClassName$()
        {}

        private static readonly $ClassName$ instance = new $ClassName$();

        public static $ClassName$ Instance
        {
            get
            {
                return instance;
            }
        }

        #endregion Fast Thread Safe Singleton Implementation
        ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
Full lazy implementation as a 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>Full Lazy Singleton</Keyword>
      </Keywords>
      <SnippetTypes>
        <SnippetType>Expansion</SnippetType>
      </SnippetTypes>
      <Title>FullLazySingleton</Title>
      <Author>Microsoft</Author>
      <Description>Make a threadsafe singleton class</Description>
      <HelpUrl>
      </HelpUrl>
      <Shortcut>FullLazySingleton</Shortcut>
    </Header>
    <Snippet>
      <Declarations>
        <Literal Editable="false">
          <ID>ClassName</ID>
          <ToolTip>
          </ToolTip>
          <Default>
          </Default>
          <Function>ClassName()</Function>
        </Literal>
      </Declarations>
      <Code Language="csharp"><![CDATA[
    #region Thread Safe Lazy Singleton Implementation
    
    private $ClassName$()
    {
    }

    public static $ClassName$ Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    
    class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly $ClassName$ instance = new $ClassName$();
    }


    #endregion Thread Safe Lazy Singleton Implementation
]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

No comments: