程序员面试题精选100题(56)-C/C++/C#面试题(4)

面试题 时间:2019-09-22 手机网站

        private static Singleton1 instance = new Singleton1();

        public static Singleton1 Instance

        {

            get

            {

                return instance;

            }

        }

    }

 

    public sealed class Singleton2

    {

        Singleton2()

        {

            Console.WriteLine("Singleton2 constructed");

        }

        public static void Print()

        {

            Console.WriteLine("Singleton2 Print");

        }

        public static Singleton2 Instance

        {

            get

            {

                return Nested.instance;

            }

        }

        class Nested

        {

            static Nested() { }

 

            internal static readonly Singleton2 instance = new Singleton2();

        }

    }