The singleton pattern is designed to restrict the instantiation of a class to one object. The advantage of doing this is that only one object is responsible for coordinating the actions across an application.
The singleton pattern though being one of the most basic software patterns suffers from being the most misused pattern, resulting in many people in the software industry referring to it as an anti pattern. An anti pattern is a design pattern that appears obvious but is ineffective or far from optimal in practice. In reality the singleton pattern could assist in improving an applications performance if it is implemented correctly and consideration is given to it’s use.
Static Initialization

The following illustration presents a very basic implementation of a singleton class.
public sealed class Singleton
{
private static readonly Singleton _instance = new Singleton();
public Singleton() { }
public static Singleton Instance
{
get
{
return _instance;
}
}
}
The instance of the class is created the first time any member of the class is referenced. The common language runtime (CLR) takes care of the variable initialization. The class is marked sealed to prevent derivation, which could add instances. The above example implements a form of instantiation which is known as Lazy Instantiation.
The only potential downside to this approach is that you are handing control of instantiating the member variable over to the CLR, so you are unable to initiate any checks on the member variable at instantiation time, however in most cases in creating a singleton method in C# this is the preferred method.
Multithreaded Singleton
Implementing a singleton class in a multi threaded environment adds a little more complication because you cannot rely on the CLR to ensure thread safety. In a multi threaded environment it is best to use C# language specific language capabilities to ensure that only one instance of the object is created in the presence of multiple threads.

public sealed class Singleton
{
private static volatile Singleton _instance;
private static readonly object _lock = new Object();
private Singleton() { }
public static Singleton Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
}
This approach ensures that only one instance is created and only when the instance is needed. Also, the variable is declared to be volatile to ensure that assignment to the instance variable completes before the instance variable can be accessed. Lastly, this approach uses a _lock instance to lock on, rather than locking on the type itself, to avoid deadlocks.
This double-check locking approach solves the thread concurrency problems while avoiding an exclusive lock in every call to the Instance property method. It also allows you to delay instantiation until the object is first accessed. In practice, an application rarely requires this type of implementation. In most cases, the static initialization approach is sufficient.
Singleton Pattern VS Static class
Although aesthetically static classes and the singleton pattern appear to provide the same type functionality , providing a mechanism for sharing of redundant objects in memory. There is however differences in the implementation. There advantages and disadvantages to both these solutions and it is entirely dependent on the situation in which you need to implement either. A big difference between Static classes and singletons is that Static classes cannot implement interfaces and you cannot pass a static class as if it were just another implementation.
Advantages of Singletons
- Singleton classes can extend classes and implement interfaces
- Singleton classes can be initialised lazily or asynchronously.
- Singleton classes can be extended and it’s methods can be overridden
- Singleton classes can be handled polymorphically without forcing their users to assume that there is only one instance.
Keep grinding the code,
Gary
Recommended Reading