What is the Singleton Pattern?

The Singleton Design Pattern is a creational pattern that ensures only one instance of a class is created and provides a global access point to that instance.

In simple terms, imagine you have a class that should only ever have one object (like a single log manager, configuration handler, or shared resource). The Singleton pattern helps you do exactly that.

When to Use Singleton Pattern?

Use Singleton when,

Basic Structure of Singleton Pattern

Key Points

Basic Example

public sealed class Singleton
{
    private static Singleton instance = null;

    // Private constructor to prevent object creation
    private Singleton()
    {
        Console.WriteLine("Singleton instance created");
    }

    // Public method to get the instance
    public static Singleton GetInstance()
    {
        if (instance == null)
        {
            instance = new Singleton();
        }
        return instance;
    }
}

Thread-Safe Singleton

In multi-threaded applications, the basic version might create multiple instances. So we make it thread-safe:

Using Lock

public sealed class Singleton
{
    private static Singleton instance = null;
    private static readonly object lockObj = new object();
    private Singleton() { }
    public static Singleton GetInstance()
    {
        lock (lockObj)
        {
            if (instance == null)
            {
                instance = new Singleton();
            }
        }
        return instance;
    }
}

Best Practice: Lazy Initialization

C# also provides an easy way using Lazy<T>.

public sealed class Singleton
{
    private static readonly Lazy<Singleton> instance = 
        new Lazy<Singleton>(() => new Singleton());
    private Singleton() { }
    public static Singleton Instance => instance.Value;
}

Eager vs Lazy Loading

Approach Description
Eager The instance is created at application start. Good for performance-critical cases.
Lazy The instance is created when it's needed. Saves memory and start-up time.


Drawbacks of Singleton

Real-Time Use Cases

Summary