| 簡便性 | 安全性 | 異常處理 | |
| 靜態成員初始化 | 最簡便 | 比較安全 | 非常難 |
| 靜態構造函數 | 比較簡便 | 最安全 | 容易 |
| public class MySingleton { PRivate static readonly MySingleton _theOneAndOnly; static MySingleton() { _theOneAndOnly = new MySingleton(); } public static MySingleton TheOnly { get{ return _theOneAndOnly; } } /// <summary> /// Private constructor to avoid object created from outside /// </summary> private MySingleton() {} } |
| class A { public static int X = B.Y; static A() { ++X; } } class B { public static int Y = A.X; static B() { ++Y; } } |
| Debug.WriteLine( A.X.ToString() ); Debug.WriteLine( B.Y.ToString() ); |
| Debug.WriteLine( A.X.ToString() ); |
新聞熱點
疑難解答