C#单例模式的三种写法
public class Singleton
{
private static Singleton _instance = null;
private Singleton(){}
public static Singleton Instance()
{
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 Instance()
{
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();
}