Create Singleton in C#
ByOne of the most basic and most useful object patterns is the Singleton design pattern. A Singleton is a class of which there will only be one instance created at any time. All users of the Singleton class will all use the same instance. This is great for a wide range of applications such as cached values and lookups or utilities that execute quickly. The Design Patterns book by Gamma et al . describes the structure of a basic Singleton. We’ll describe how to create one in C#.
The classic approach to creating a Singleton looks like this:
public class SingletonGamma { private static SingletonGamma instance; private SingletonGamma() { } public static SingletonGamma Instance { get
{
if (instance == null) { instance = new SingletonGamma(); }
return instance;
}
}
}
The basic approach is to create a private constructor and an Instance method that returns the current instance if available, or returns a new instance if there is no current instance. We call that "lazy instantiation". The problem with this approach in C# is that it is not strictly thread-safe. That is, it is possible for 2 threads to create separate instances is some circumstances. This is not what we are looking to do. This approach does have an advantage in that there is a hook-point to do post-instantiation processing if required. In .Net though, this is not the approach usually recommended
The following example takes advantage of the better handling of variable ambiguity in .Net (vs. C++) to create a thread-safe Singleton:
public sealed class SingletonCSharp { private static readonly Singleton instance = new SingletonCSharp(); private SingletonCSharp() { } public static SingletonCSharp Instance { get { return instance; } } }
This is the preferred approach in C# and is adequate for most programs. There are other approaches that allow for more flexibility and still maintain thread safety, but they introduce unnecessary complexity for most programs. Note the read only instance property that can only be set during instantiation and the sealed keyword that prevents inheritance of the class which could introduce unwanted side effects, such as secondary instances. This is a simple and straightforward approach to the design pattern. The only downside is the lack of flexibility to have non-standard constructors. For most applications, that will not be an issue.
Related posts:

