コード例 #1
0
        public static SingletonExample Instance()
        {
            // Lazy initialization:

            if (_instance == null)
            {
                _instance = new SingletonExample();
            }

            return(_instance);
        }
コード例 #2
0
        static void Main(string[] args)
        {
            //Globally accessible single object instance
            //Singleton vs Static Data
            SingletonExample s1 = SingletonExample.Instance();
            SingletonExample s2 = SingletonExample.Instance();

            // Test for same instance

            if (s1 == s2)
            {
                Console.WriteLine("The Objects are the same instance");
            }

            Console.ReadKey();
        }