コード例 #1
0
        static void Main(string[] args)
        {
            //SingletonClass.getInstance();
            //SingletonClass.getInstance();
            //SingletonClass.getInstance();

            LazySingleton a = LazySingleton.getInstance();
            LazySingleton b = LazySingleton.getInstance();

            if (a == b)
            {
                Console.WriteLine("Hello World!");  //the best check SHALLOW check,
            }
            //if they are pointed to the same object


            //SingletonClass sc = new SingletonClass(); //cannot access
        }
コード例 #2
0
 public static LazySingleton getInstance()
 {
     if (instance == null)
     {
         instance = new LazySingleton();
         Console.WriteLine("created 1");
     }
     return(instance);
     //if (instance != null)
     //{
     //    Console.WriteLine("instance returned");
     //    return instance;
     //}
     //else
     //{
     //    instance = new SingletonClass();
     //    Console.WriteLine("created 1");
     //    return instance;
     //}
 }