C# Singleton Code Snippet

One of the coolest features on Visual Studio 2005 are code snippets and how easy is to add your personal snippets.

Today Raffaele Rialdi, one of our MVP, has shared one of its code snippets, created for building a Singleton class with C#. Just place the following code into the My Documents\Visual Studio 2005\Code Snippets\Visual C#\My Code Snippets folder and use it on the code window by writing singleton<tab> on a C# class:

?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="
http://atd.vevy.com/CodeSnippet">
 <CodeSnippet Format="1.0.0">
  <Header>
   <Title>singleton</Title>
   <Shortcut>singleton</Shortcut>
   <Description>Code snippet for singleton pattern</Description>
   <Author>Raffaele Rialdi</Author>
   <SnippetTypes>
    <SnippetType>Expansion</SnippetType>
   </SnippetTypes>
  </Header>
  <Snippet>
   <Declarations>
    <Literal Editable="false">
     <ID>classname</ID>
     <ToolTip>Class name</ToolTip>
     <Function>ClassName()</Function>
     <Default>ClassNamePlaceholder</Default>
    </Literal>
   </Declarations>
   <Code Language="csharp">
    <![CDATA[private static $classname$ _Instance = null;
  private $classname$ ()
  {
  }
  public static $classname$ GetInstance()
  {
   if(_Instance == null)
    _Instance = new $classname$();
   return _Instance;
  }

$end$
]]>
   </Code>
  </Snippet>
 </CodeSnippet>
</CodeSnippets>

Really useful...

Print | posted on Friday, January 20, 2006 5:52 PM

Comments on this post

# re: C# Singleton Code Snippet

Requesting Gravatar...
That was inspiring,

thanks to your post know I can use snippet better than ever

thanks for the post
Left by software developers on Aug 14, 2009 10:28 AM

Your comment:

 (will show your gravatar)
 
Please add 3 and 6 and type the answer here: