C# 单例模式使用 Singleton

 Singleton 类如下:

    public class Singleton<T> where T : class, new()
    {
        private static T _instance;

        private static readonly object syslock = new object();

        public static T getInstance()
        {
            if (_instance == null)
            {
                lock (syslock)
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                    }
                }
            }
            return _instance;
        }
    }

如要实现单例的继承 Singleton

    public class T1 : Singleton<T1>
    {
        public void Test()
        {
        }
    }

调用 

T1.getInstance().Test();

 

posted @ 2020-05-17 15:26  天天代码码天天  阅读(7)  评论(0编辑  收藏  举报  来源