zoukankan      html  css  js  c++  java
  • C#单例模式的三种写法

    第一种……

    public class Singleton
    {
        private static Singleton _instance = null;
        private Singleton(){}
        public static Singleton CreateInstance()
        {
            if(_instance == null)

            {
                _instance = new Singleton();
            }
            return _instance;
        }
    }

    另外一种

    public class Singleton
    {
        private volatile static Singleton _instance = null;
        private static readonly object lockHelper = new object();
        private Singleton(){}
        public static Singleton CreateInstance()
        {
            if(_instance == null)
            {
                lock(lockHelper)
                {
                    if(_instance == null)
                         _instance = new Singleton();
                }
            }
            return _instance;
        }
    }

    第三种

    public class Singleton
    {

        private Singleton(){}
        public static readonly Singleton instance = new Singleton();
    }  

  • 相关阅读:
    4. Object
    3. string
    8. 滚动条美化
    7. 单位,移动布局
    2. js的异步
    2. 即时通讯
    让PHP更快的提供文件下载
    phpstorm+xdebug远程调试设置
    postman自动生成签名
    一位技术人员成长历程
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7003456.html
Copyright © 2011-2022 走看看