Visual Studio 2005 Singleton Snippet

Intelligent code snippets, once the domain of third-party refactoring tools, are now available natively in Visual Studio 2005. Intelligent code snippets allow you to write templates that can be used to insert common code into the current code editor. CSharpCorner has a good little article describing code snippets right here.

As an example of what you can do (and because I’m such a nice guy), here is a snippet that you can use to implement singleton behaviour in a brand new class. The way I use it is to create a new, empty class, then insert this snippet. You’ll get a static singleton object, a private constructor, and nice GetInstance method, all with references to the current class name, and with no typing at all. Nice.

The snippet itself shows how you can use the built-in ClassName() function inside the snippet to insert the current classname where required. The other functions that might come in useful are GenerateSwitchCases, ListVariablesOfType, ListEnumVariables, and SimpleTypeName. Get your snippets on!

Join the Conversation


  1. I like that, its cute.

    Have you thought about implementing it with a thread-safe singleton?:
    public readonly static MyClass Instance = new MyClass();


Leave a comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: