



















第一种、双锁定法
public sealed class Singleton
{
static Singleton instance = null;
static readonly object lockhelper = new object();Singleton()
{
}public static Singleton Instance
{
get
{
if (instance == null)
{
lock (lockhelper)
{
if (instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
}
第二种、静态初始化
public sealed class Singleton
{
static readonly Singleton instance = new Singleton();static Singleton()
{
}Singleton()
{
}public static Singleton Instance
{
get
{
return instance;
}
}
}
第三种、延时初始化
public sealed class Singleton
{
Singleton()
{
}public static Singleton Instance
{
get
{
return Nested.instance;
}
}class Nested
{
static Nested()
{
}internal static readonly Singleton instance = new Singleton();
}
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。