首页 > Windows开发 > 详细

C# 单例模式

时间:2017-08-20 18:00:33      阅读:266      评论:0      收藏:0      [点我收藏+]
        #region Singleton
        class Singleton
        {
            private static Singleton singleton;
            private static object lockObj = new object();
            private Singleton()
            {
            }
            public static Singleton GetInstance()
            {
                if (singleton == null)
                {
                    lock (lockObj)
                    {
                        if (singleton == null)
                        {
                            singleton = new Singleton();
                        }
                    }
                }
                return singleton;
            }
        }
        static void SingletonTest()
        {
            Singleton s1 = Singleton.GetInstance();
            Singleton s2 = Singleton.GetInstance();
            if (s1 == s2)
            {
                Console.WriteLine("The same instance");
            }
        }
        #endregion   

 

C# 单例模式

原文:http://www.cnblogs.com/zxxxx/p/7400502.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!