c#单例模式(Singleton)的6种实现

2019-12-30 15:36:35王振洲

最后我们在Test类中添加一个静态构造函数如下:


 class Test
{
  public static string x = EchoAndReturn("In type initializer");

  static Test()
  {
  }

  public static string EchoAndReturn(string s)
  {
    Console.WriteLine(s);
    return s;
  }
}
 

c#,单例模式实现,c#单例模式,c#单例模式应用

图7 输出结果

理论上,type initializer应该发生在”Echo!”之后和”After echo”之前,但这里却出现了不唯一的结果,只有当Test类包含静态构造函数时,才能确保type initializer的初始化发生在”Echo!”之后和”After echo”之前。

所以说要确保type initializer发生在被字段引用时,我们应该给该类添加静态构造函数。接下来让我们介绍单例模式的静态方式。

 静态初始化

 


public sealed class Singleton
{
  private static readonly Singleton _instance = new Singleton();

  // Explicit static constructor to tell C# compiler
  // not to mark type as beforefieldinit
  static Singleton()
  {
  }

  /// <summary>
  /// Prevents a default instance of the 
  /// <see cref="Singleton"/> class from being created.
  /// </summary>
  private Singleton()
  {
  }

  /// <summary>
  /// Gets the instance.
  /// </summary>
  public static Singleton Instance
  {
    get
    {
      return _instance;
    }
  }
}

以上方式实现比之前介绍的方式都要简单,但它确实是多线程环境下,C#实现的Singleton的一种方式。由于这种静态初始化的方式是在自己的字段被引用时才会实例化。

 让我们通过IL代码来分析静态初始化。

c#,单例模式实现,c#单例模式,c#单例模式应用

图8静态初始化IL代码

首先这里没有beforefieldinit的修饰符,由于我们添加了静态构造函数当静态字段被引用时才进行初始化,因此即便很多线程试图引用_instance,也需要等静态构造函数执行完并把静态成员_instance实例化之后可以使用。

 延迟初始化


 /// <summary>
/// Delaies initialization.
/// </summary>
public sealed class Singleton
{
  private Singleton()
  {
  }

  /// <summary>
  /// Gets the instance.
  /// </summary>
  public static Singleton Instance { get { return Nested._instance; } }

  private class Nested
  {
    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Nested()
    {
    }

    internal static readonly Singleton _instance = new Singleton();
  }
}