C# Singleton

Summary: in this tutorial, you will learn how to use the C# Singleton pattern to ensure a class has only one instance.

Introduction to the C# Singleton pattern

Sometimes, you need to create one and only one instance of a class. The reason is that creating multiple instances of such a class may lead to problems like incorrect data or resource contention.

For example, when you work with a logging framework or database connection.

The Singleton pattern solves the problem of creating a single instance of a class that should be globally accessible throughout the application.

The Singleton pattern is a creational design pattern that ensures a class has only one instance and provides a global access point to that instance.

The UML diagram below illustrates the Singleton design pattern.

The Singleton pattern consists of a single class responsible for creating and maintaining a single instance.

The participants in the Singleton pattern are:

  • Singleton: is a class for creating and maintaining a single instance.
  • Client: is the class that uses the Singleton instance.

The Singleton class is responsible for creating a single instance. The Client class gets the Singleton instance by calling the Instance accessor or GetInstance() method.

The Singleton class has a constructor marked as private to prevent other classes from creating instances of the Singleton class.

The Singleton class also has an accessor that provides access to the instance.

Singleton Pattern Pros & Cons

The Singleton pattern offers several benefits, including:

  • Ensure only one instance of the class.
  • Provide a global point of access to the instance.
  • Provide a way to control access to the instance.

In addition to the benefits mentioned above, the Singleton pattern has several drawbacks:

  • Difficult to test. The Singleton pattern can make testing more difficult. The Singleton pattern creates a global point of access to the instance, which leads to unexpected dependencies in test cases.
  • Lead to tight coupling between classes. Classes that depend on the Singleton instance are aware of its existence and how to access it, which may lead to a strong coupling between the classes. As a result, it’s challenging to change the Singleton class or the classes that depend on the Singleton class without affecting other parts of the system.

Singleton implementation in C#

The following illustrates how to use implement the Singleton pattern in C#:

sealed class Singleton
{
    private static Singleton? _instance;

    public static Singleton Instance
    {
        get
        {
            _instance ??= new Singleton();
            return _instance;
        }
    }

    private Singleton()
    {
    }
}Code language: C# (cs)

How it works.

First, define the Singleton class with the sealed keyword so that other classes will not be able to inherit from this class.

Second, declare a static private field with the type Singleton called _instance.

Third, define the accessor that creates an instance of the Singleton class if it is null; otherwise, return the instance. This accessor is the only way to access the Singleton instance.

Finally, mark the constructor of the Singleton class as private to prevent other classes from creating a new instance of the Singleton class.

C# Singleton example

The following program demonstrates how to apply the Singleton pattern to define a Logger class that has one and only one instance throughout the application:

namespace DP;

sealed class Logger
{
    private static Logger? _instance;
    public static Logger Instance
    {
        get
        {
            _instance ??= new Logger();
            return _instance;
        }
    }

    private Logger()
    {
    }

    public void Log(string message)
    {
        Console.WriteLine(message);
    }
}

class Program
{
    public static void Main(string[] args)
    {
        var logger = Logger.Instance;
        logger.Log("C# Singleton");
    }
}Code language: C# (cs)

Output:

C# SingletonCode language: C# (cs)

Singleton applications

Here are some practical applications of the Singleton pattern:

  • Managing resources such as thread pools, database connections, and caches.
  • Logging and auditing frameworks that need to maintain a single log or audit trail throughout the system.
  • Configuration management, where you need one and only one instance of the configuration manager that is accessible from any part of the system.

Summary

  • Use the Singleton pattern to ensure that a class has one and only one instance through the application.
  • The Singleton can make the application difficult to test and may lead to tight coupling between classes.
Was this tutorial helpful ?