🤖▶️ Check out the Design Patterns Overview course by Steve @ardalis Smith!Check it out »Hide

DevIQ

Singleton

Singleton

The Singleton design pattern is used to ensure an application never contains more than a single instance of a given type. It is often considered to be an antipattern, since the pattern's implementation places the responsibility of enforcing the single instance behavior on the type itself. This violates the Single Responsibility Principle and references to the type's static Instance property often result in tight coupling (see Static Cling).

An example implementation:

// Bad code! Do not use!
// See http://csharpindepth.com/Articles/General/Singleton.aspx
public sealed class Singleton
{
private static Singleton instance=null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}

A better approach than the Singleton pattern is to follow the Explicit Dependencies Principle from for classes that depend on a singleton type. Use dependency injection to pass the object into the type that needs it, and configure the services/IOC container to enforce the object's singleton lifetime behavior.

References

Singleton Design Pattern in C# - Pluralsight

Jon Skeet on Implementing the Singleton Pattern in C#

Edit this page on GitHub

On this page

Sponsored by NimblePros
Sponsored