//第一种实现方式
public class Singleton
{
private static Singleton Instance;
private Singleton(){}
public static Singleton GetInstance()
{
if(Instance==null)
{
Instance=new SingLeton();
return Instance;
}
return Instance;
}
}
//第二种实现方式
public class Singleton
{
private static Singleton Instance;
private static readonly object locker= new object();
private Singleton(){}
public static Singleton GetInstance()
{
lock(locker)
{
if(Instance==null)
{
Instance=new Singleton();
return Instance;
}
}
return Instance;
}
原文:https://www.cnblogs.com/clhxxlcj/p/10916779.html