惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
博客园_首页
M
MIT News - Artificial intelligence
雷峰网
雷峰网
GbyAI
GbyAI
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
S
SegmentFault 最新的问题
H
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
H
Hackread – Cybersecurity News, Data Breaches, AI and More
博客园 - 【当耐特】
V
Visual Studio Blog
月光博客
月光博客
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
云风的 BLOG
云风的 BLOG
美团技术团队
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
有赞技术团队
有赞技术团队

博客园 - zeus2

Xfire的初次使用 SQl Server 2012正式版发布 Oracle常用Hint Oracle 设计海量数据库 读书笔记(三) Oracle 设计海量数据库 读书笔记(二) Oracle 设计海量数据库 读书笔记(一) 解决中文ID3标签乱码zz 系统架构性能提高方案! 使用开源工具架设开发平台 修改SQL Server数据库地址 System.DateTimeOffset Load的问题 关于__doPostBack之前截获调用 - zeus2 - 博客园 当应用程序发布到iis7/iis7.5出现需要使用经典模式时 - zeus2 - 博客园 [读书笔记]SQL技术内幕Identity XML序列化封装 根据实体类生成查询安全版 生活太艰难了。!!! 从底层角度看ASP.NET-A low-level Look at the ASP.NET Architecture(转载) C++访问Sqlite数据库(存档) - zeus2 - 博客园
单例模式的三种实现方法
zeus2 · 2012-03-04 · via 博客园 - zeus2

第一种、双锁定法 

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();
}
}