第一种方式
public class Singleton
{
private Singleton()
{
Console.Write(".");
}
private static Singleton _instance;
private static readonly object syn = new object();
public static Singleton GetInstance()
{
if (_instance == null)
{
lock (syn)
{
if (_instance == null)
{
_instance = new Singleton();
}
}
}
return _instance;
}
}
第二种方式
public class Singleton2
{
private Singleton2()
{
Console.Write("1");
}
private static readonly Singleton2 _instance = new Singleton2();
public static Singleton2 GetInstance()
{
return _instance;
}
}