标签:c#
第一种……
public class Singleton
{
private static Singleton _instance = null;
private Singleton(){}
public static Singleton CreateInstance()
{
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 CreateInstance()
{
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();
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:c#
原文地址:http://blog.csdn.net/u012842807/article/details/47780509