SoFunction
Updated on 2025-03-03

Implementation method of C# singleton class

A singleton class ensures that a class has only one instance globally and provides a global access point. Since only one instance can be generated, we must set the constructor as a private function to prohibit others from creating instances.

Implementation 1: lazy, thread-unsafe

This implementation can be used without additional overhead and does not require thread safety:

public class Singleton1
{
  private static Singleton1 instance = null;
  private Singleton1() { }

  public static Singleton1 Instance
  {
    get
    {
      if (instance == null)
      {
        instance = new Singleton1();
      }
      return instance;
    }
  }
}

Implementation 2: lazy, thread-safe

Since locking is added every time you access a singleton instance, and locking is a very time-consuming operation, it is not recommended to use:

public class Singleton2
{
  private readonly static object lockObj = new object();
  private static Singleton2 instance = null;
  private Singleton2() { }

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

Implementation 3: Hungry Man-style, thread-safe

The writing is simple and thread-safe, but the construction timing is not controlled by the programmer:

public class Singleton3
{
  private static Singleton3 instance = new Singleton3();
  private Singleton3() { }
  public static Singleton3 Instance { get { return instance; } }

  public static void Test()
  {
    ("test");
  }
}

When the .NET runtime finds that the first time using Singleton3 is used, an instance of the singleton will be created instead of when the property is called for the first time, such as:

();

Implementation 4: lazy style, double verification lock

Improvements are made based on implementation 2, and only locks are added when the instance is created for the first time to improve access performance:

public class Singleton4
{
  private readonly static object lockObj = new object();
  private static Singleton4 instance = null;
  private Singleton4() { }

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

Implementation 5: lazy, internal class

Improvements are made based on method 3 to ensure that the instance is constructed only when the attributes are accessed:

public class Singleton5
{
  class Nested 
  {
    internal static readonly Singleton5 instance = new Singleton5();
  }
  private Singleton5() { }
  public static Singleton5 Instance { get { return ; } }
}

Implement singleton base class

Through the singleton base class, we can simply create a singleton class through inheritance to implement code reuse:

// Since the singleton base class cannot be instantiated, it is designed as an abstract classpublic abstract class Singleton<T> where T : class
{
  // Here, implement the solution of realizing 5, and any of the above solutions can be actually adopted  class Nested
  {
    // Create a template class instance, setting parameter 2 to true means that private constructors are supported    internal static readonly T instance = (typeof(T), true) as T;
  }
  private static T instance = null;
  public static T Instance { get { return ; } }
}

How to use it is as follows:

class TestSingleton : Singleton<TestSingleton>
{
  //Private the constructor to prevent external creation through new  private TestSingleton() { }
}

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.