1 using System;
2
3 namespace Singleton
4 {
5 /// <summary>
6 /// 作者:bzyzhang
7 /// 时间:2016/5/30 22:10:39
8 /// 博客地址:http://www.cnblogs.com/bzyzhang/
9 /// Singleton说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址
10 /// </summary>
11 public class Singleton
12 {
13 ///实现一
14 //private static Singleton instance;
15
16 //private Singleton()
17 //{
18 //}
19
20 //public static Singleton GetInstance()
21 //{
22 // if (instance == null)
23 // {
24 // instance = new Singleton();
25 // }
26 // return instance;
27 //}
28
29 /// <summary>
30 /// 实现二,多线程时的单例
31 /// </summary>
32 //private static Singleton instance;
33
34 //private static readonly object syncRoot = new object();
35
36 //private Singleton() { }
37
38 //public static Singleton GetInstance()
39 //{
40 // lock (syncRoot)
41 // {
42 // if (instance == null)
43 // {
44 // instance = new Singleton();
45 // }
46 // }
47 // return instance;
48 //}
49
50 /// <summary>
51 /// 实现三,双重锁定
52 /// </summary>
53 //private static Singleton instance;
54
55 //private static readonly object syncRoot = new object();
56
57 //private Singleton() { }
58
59 //public static Singleton GetInstance()
60 //{
61 // if (instance == null)
62 // {
63 // lock (syncRoot)
64 // {
65 // if (instance == null)
66 // {
67 // instance = new Singleton();
68 // }
69 // }
70 // }
71 // return instance;
72 //}
73
74 /// <summary>
75 /// 实现四,静态初始化
76 /// </summary>
77 private static readonly Singleton instance = new Singleton();
78
79 private Singleton() { }
80
81 public static Singleton GetInstance()
82 {
83 return instance;
84 }
85 }
86 }