Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Singleton Pattern in C#

Introduction

The Singleton Pattern is a design pattern that restricts the instantiation of a class to one single instance. This is useful when exactly one object is needed to coordinate actions across the system. The Singleton Pattern ensures that a class has only one instance and provides a global point of access to it.

When to Use Singleton Pattern

The Singleton Pattern is typically used when:

  • There must be exactly one instance of a class, and it must be accessible from a well-known access point.
  • The single instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

Implementation in C#

Here is a step-by-step guide to implementing the Singleton Pattern in C#:

// Step 1: Create a class and make its constructor private
public class Singleton
{
    private static Singleton instance = null;
    private static readonly object padlock = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
                return instance;
            }
        }
    }
}
                

In this example:

  • The constructor is private, which means that the class cannot be instantiated from outside the class.
  • A static variable instance holds the single instance of the class.
  • A static readonly object padlock is used to ensure thread safety.
  • The Instance property provides a global point of access to the Singleton instance and ensures that only one instance is created.

Example Usage

Below is an example of how to use the Singleton pattern in a C# application:

class Program
{
    static void Main(string[] args)
    {
        Singleton s1 = Singleton.Instance;
        Singleton s2 = Singleton.Instance;

        if (s1 == s2)
        {
            Console.WriteLine("Both instances are the same.");
        }
        else
        {
            Console.WriteLine("Instances are different.");
        }
    }
}
                

Output:

Both instances are the same.

In this example, s1 and s2 refer to the same instance of the Singleton class.

Thread Safety in Singleton

Ensuring thread safety is crucial when implementing the Singleton pattern in a multi-threaded environment. The implementation provided above uses a lock statement to achieve thread safety. The lock keyword ensures that one thread does not enter a critical section of code while another thread is in the critical section.

Conclusion

The Singleton Pattern is a powerful design pattern that ensures a class has only one instance and provides a global point of access to it. Properly implementing a Singleton in C# involves making the constructor private, using a static variable to hold the single instance, and ensuring thread safety with a lock object.

By understanding and applying the Singleton Pattern, you can ensure that your class has only one instance, providing a consistent and controlled access point throughout your application.