示例#1
0
 public static NewSingleton Instance()
 {
     if (_instance == null)
     {
         lock (syncLock)
         {
             if (_instance == null)
             {
                 _instance = new NewSingleton();
             }
         }
     }
     return(_instance);
 }
示例#2
0
        static void Main(string[] args)
        {
            OldSingleton os1 = OldSingleton.Instance();
            OldSingleton os2 = OldSingleton.Instance();

            os1.Name = "This is old singleton class!!";
            Console.WriteLine(os2.Name);

            NewSingleton ns1 = NewSingleton.Instance();
            NewSingleton ns2 = NewSingleton.Instance();

            ns1.Name = "This is new singleton class!!";
            Console.WriteLine(ns2.Name);

            OptimizeSingleton ops1 = OptimizeSingleton.Instance();
            OptimizeSingleton ops2 = OptimizeSingleton.Instance();

            ops1.Name = "This is new C# optimize singleton class!!";
            Console.WriteLine(ops2.Name);

            Console.ReadKey();
        }