”靠谱的C#“单例模式

//静态构造函数的单例模式

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


    static Singleton()
    {
    }

    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return _instance;
        }
    }
}

  

//延迟构造对象的单例模式

/// <summary>
/// 在.Net4.0或以上版本才行
/// </summary>
public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
}

  

原文地址:https://www.cnblogs.com/zzq-include/p/4309186.html