”靠谱的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()
    {
    }
}

  

posted @ 2015-03-02 16:48  soft.push("zzq")  Views(188)  Comments(0)    收藏  举报