📜  单例设计模式 c# volatile - C# 代码示例

📅  最后修改于: 2022-03-11 14:48:47.286000             🧑  作者: Mango

代码示例1
public sealed class Singleton
{
   private static volatile Singleton instance;
   private static object syncRoot = new Object();

   private Singleton() {}

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

         return instance;
      }
   }
}