单例模式简单实现
1 class Singleton 2 { 3 public static readonly Singleton singleton=new Singeton(); 4 5 static Singleton() 6 { 7 } 8 public static Singleton GetInstance() 9 { 10 return singleton; 11 } 12 }
singleton 是一个静态只读对象,不属于任何的实例,所以可以保证单例。静态构造函数只能由CLR调用。
原文:http://www.cnblogs.com/clark-lee/p/3613545.html