Пример #1
0
        /// <summary>
        /// Method to return instance
        /// </summary>
        /// <returns>returns instance</returns>
        public static LazySingleton GetInstance()
        {
            ////if instance is null, initialize
            if (instance == null)
            {
                instance = new LazySingleton();
            }

            return(instance);
        }
Пример #2
0
        /// <summary>
        /// Function for lazy singleton
        /// </summary>
        public static void LazyInstance()
        {
            ////creating instances of EagerSingleton
            LazySingleton instanceOne = LazySingleton.GetInstance();
            LazySingleton instanceTwo = LazySingleton.GetInstance();

            ////checking if all the instances are same
            if (instanceOne == instanceTwo)
            {
                Console.WriteLine("Both instances are same");
                Console.WriteLine("instanceOne hashcode is " + instanceOne.GetHashCode());
                Console.WriteLine("instanceTwo hashcode is " + instanceTwo.GetHashCode());
            }
        }