当前位置: 代码迷 >> .NET相关 >> Singleton<T>
  详细解决方案

Singleton<T>

热度:551   发布时间:2016-04-24 02:58:14.0
Singleton<T>

代码如下:

    public class Singleton<T> where T : class    {        private static T _instance;        private static readonly object _lock = new object();        public static T Instance        {            get            {                if (_instance == null)                {                    lock (_lock)                    {                        if (_instance == null)                        {                            _instance = (T)Activator.CreateInstance(typeof(T), true);                        }                    }                }                return _instance;            }        }    }

使用:

    public class User : Singleton<User>    {        private User() { }    }

 

Implementing the Singleton Pattern in C# 中文版

  相关解决方案