C#经典的线程安全的单例模式(独身模式)实现代码
publicsealedclassSingleton{Singleton(){}publicstaticSingletonInstance{get{returnNested.instance;}}classNested{// Explicit static constructor to tell C# compiler// not to mark type as beforefieldinitstaticNested(){}internalstaticreadonlySingleton instance =newSingleton();}}
使用内嵌类,实现延迟加载;指定为readonly保证线程安全。
浙公网安备 33010602011771号