单例模式 (Singleton),保证一个类只有一个实例,并提供一个访问他的全局访问点。

class Singleton { private static Singleton instance; private static readonly object syncRoot = new object(); private Singleton() { } public static Singleton GetInstance() { if (instance == null) { lock (syncRoot) { if (instance == null) { instance = new Singleton(); } } } return instance; } }