简便性 | 安全性 | 异常处理 | |
静态成员初始化 | 最简便 | 比较安全 | 非常难 |
静态构造函数 | 比较简便 | 最安全 | 容易 |
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() ); |
新闻热点
疑难解答