Пример #1
0
        static void Main(string[] args)
        {
            var instance = SingletonSample.GetInstance();

            instance?.Print();
            Console.WriteLine("exit");
        }
Пример #2
0
        /// <summary>
        /// 使用方法获取单例实例
        /// </summary>
        /// <returns></returns>
        public static SingletonSample GetInstance()
        {
            if (_instance != null)
            {
                return(_instance);
            }

            lock (InstanceLocker)
            {
                if (_instance == null)
                {
                    _instance = new SingletonSample();
                }
            }

            return(_instance);
        }