只能生成一個實例的類是實現了Singleton(單例)模式的類。如下爲C#實現單例模式的方式。 方式一隻使用於單線程環境 // 把構造函數設爲私有函數以禁止他人建立實例 // 定義一個靜態的實例在須要的時候建立該實例 // 在Singlrton的靜態屬性Instance中只有在instance爲null的時候才建立一個實例以免 // 重複建立 // 把構造函數定義爲私有函數 public sealed class Singleton1 { public int a = 2; private Singleton1() { } private static Singleton1 instance = null; public static Singleton1 Instance { get { if (instance == null) instance = new Singleton1(); return instance; } } } 方式二雖然在多線程環境中能工做但效率不高 // 每次經過屬性Instance獲得Singleton2的實例都會試圖加上一個同步鎖 // 而加鎖是一個很是耗時的操做在沒有必要的時候應該儘可能避免 public sealed class Singleton2 { public int a = 2; private Singleton2(){} private static readonly object syncObj = new object(); private static Singleton2 instance = null; public static Singleton2 Instance { get { lock (syncObj) { if (instance == null) instance = new Singleton2(); } return instance; } } } 可行的解法 加同步鎖先後兩次判斷實例是否已存在 // 只有instance爲null即沒有建立時須要加鎖操做。 public sealed class Singleton3 { private Singleton3() { } private static readonly Object syncObj = new Object(); private static Singleton3 instance = null; public static Singleton3 Instance { get { if(instance == null) { lock(syncObj) { if(instance == null) instance = new Singleton3(); } } return instance; } } } 推薦的解法一利用靜態構造函數 // 在初始化靜態變量instance的時候建立一個實例 // 因爲C#是在調用靜態構造函數時初始化靜態變量.NET運行時可以確保只調用一次靜態構造 // 函數保證只初始化一次instance public sealed class Singleton4 { private Singleton4() { } private static Singleton4 instance = new Singleton4(); public static Singleton4 Instance { get { return instance; } } } 推薦的解法二 實現按需建立實例 // 在內部定義了一個私有類型Nested。 // 當第一次用到這個嵌套類的時候會調用靜態構造函數建立Singleton5的實例instance public sealed class Singleton5 { private Singleton5() { } public static Singleton5 Instance { get { return Nested.instance; } } class Nested { static Nested() { } internal static readonly Singleton5 instance = new Singleton5(); } } 擴展 定義一個表示總統的類型President能夠從該類型繼承出FrenchPresident 和AmericanPresident等類型。這些派生類型都只能產生一個實例 public class President { private string name = ""; private string country = ""; public President() { } public string Name { get { return name; } set { name = value; } } public string Country { get { return country; } set { country = value; } } } public sealed class FrenchPresident: President { private FrenchPresident():base() { } private static FrenchPresident instance = new FrenchPresident(); public static FrenchPresident Instance { get { return (FrenchPresident)(Nested.instance); } } private class Nested { static Nested() { } internal static readonly FrenchPresident instance = new FrenchPresident(); } } public sealed class AmericanPresident : President { private AmericanPresident() : base() { } private static AmericanPresident instance = new AmericanPresident(); public static AmericanPresident Instance { get { return Nested.instance; } } private class Nested { static Nested() { } internal static readonly AmericanPresident instance = new AmericanPresident(); } } 實現泛型單例模式 public class SingletonExample<T> where T : class, new() { public static T Instance { get { return Nested.instance; } } private class Nested { static Nested() { } internal static readonly T instance = new T(); } } public class Two: SingletonExample<Two> { public int a = 2; public void Show() { Console.WriteLine(a); } }