Loading

性能比较好的单例写法

2019/10/27, .Net c#代码片段

摘要:一种性能比较好的单例写法
参考来源

其他单例思路:
1.使用依赖注入,注册为单例模式
2.使用双重锁机制

public sealed class SingletonBase//应该使用密封类防止派生
{
    //写单例的方法
    //public string Getxxx(){ }
    private SingletonBase()
    {
    }

    public static SingletonBase Instance
    {
        get
        {
            return Nested.instance;
        }
    }
    private class Nested
    {
        //显式静态构造函数
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly SingletonBase instance = new SingletonBase();
    }
}
posted @ 2019-10-27 17:20  kasnti  阅读(348)  评论(0编辑  收藏  举报