Singleton模式的实现方式 C#

单例模式标准写法 (注意volatile不能少掉):

public sealed class Singleton
{
    static <span style="color:#ff0000;">volatile </span>Singleton instance=null;
    static readonly object padlock = new object();

    Singleton()
    {
    }

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



http://www.yoda.arachsys.com/csharp/singleton.html

http://stackoverflow.com/questions/1964731/the-need-for-volatile-modifier-in-double-checked-locking-in-net




你可能感兴趣的:(Singleton模式的实现方式 C#)