Introduction

In many applications, we need only one object of a particular class. Creating multiple objects may lead to wrong results, wasted memory, or conflicts.

The Singleton Design Pattern solves this problem by ensuring that only one instance of a class exists during the application lifetime.

What Is the Singleton Pattern?

The Singleton pattern:

In simple words: Create once, use everywhere.

Real-Life Examples

Here are some practical use cases:

In all these cases, creating multiple objects makes no sense.

Singleton Implementation in C#

Below is a simple, thread-safe Singleton implementation using basic C# concepts.

Singleton Class

namespace DesignPattern
{
    public sealed class SingleTonClass
    {
        private static readonly object _obj = new object();
        private static SingleTonClass _instance = null;

        private int _value = 0;

        private SingleTonClass()
        {
        }

        public static SingleTonClass GetInstance()
        {
            if (_instance == null)
            {
                lock (_obj)
                {
                    if (_instance == null)
                    {
                        _instance = new SingleTonClass();
                    }
                }
            }
            return _instance;
        }

        public void IncrementValue()
        {
            _value++;
        }

        public int GetValue()
        {
            return _value;
        }
    }
}

Code Explanation (Step by Step)

sealed class SingleTonClass

private static SingleTonClass _instance

private static readonly object _obj

private SingleTonClass()

GetInstance() Method

_value as Instance Variable

Testing the Singleton

using System;

namespace DesignPattern
{
    public class Program
    {
        static void Main(string[] args)
        {
            var s1 = SingleTonClass.GetInstance();
            s1.IncrementValue();
            s1.IncrementValue();

            var s2 = SingleTonClass.GetInstance();
            s2.IncrementValue();
            s2.IncrementValue();

            Console.WriteLine(s2.GetValue()); // 4
            Console.WriteLine(s1.GetValue()); // 4

            if (s1 == s2)
            {
                Console.WriteLine("Both are same instance");
            }

            Console.WriteLine("Happy,Coding!");
        }
    }
}

Output Explanation

Why Both s1 and s2 Have the Same Value (4)

Although s1 and s2 look like different variables, they both reference the same Singleton instance.

Step-by-Step Value Flow

_value belongs to the single object. Any change through one reference is visible through the other.

That is why:

s1.GetValue(); // 4
s2.GetValue(); // 4

Why This Proves Singleton Works

Advantages

Disadvantages

When to Use Singleton?

Use it when:

Avoid it when:

Conclusion

The Singleton Design Pattern is a fundamental and widely used pattern in C#. This simple implementation clearly shows how one instance maintains shared state across the application.

Understanding this pattern builds a strong foundation for writing clean, efficient, and maintainable code.

Happy Coding!