What is a singleton in C#?

Sergio Tapia picture Sergio Tapia · Jan 28, 2010 · Viewed 173.6k times · Source

What is a Singleton and when should I use it?

Answer

Daniel May picture Daniel May · Jan 28, 2010

A singleton is a class which only allows one instance of itself to be created - and gives simple, easy access to said instance. The singleton premise is a pattern across software development.

There is a C# implementation "Implementing the Singleton Pattern in C#" covering most of what you need to know - including some good advice regarding thread safety.

To be honest, It's very rare that you need to implement a singleton - in my opinion it should be one of those things you should be aware of, even if it's not used too often.