Пример #1
0
        public static OldSingleton Instance()
        {
            if (_instance == null)
            {
                _instance = new OldSingleton();
            }

            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();
        }