S1.
private static SingleInstance _instance = null;
public static SingleInstance Instance1 {
get { return _instance ?? (_instance = new SingleInstance()); }
}
S2.
private readonly static SingleInstance _instance = new SingleInstance();
public static SingleInstance Instance2 {
get { return _instance; }
}
s3. 多线程情况下
private static object _singleInstanceLocker = new object();
public static SingleInstance Instance3 {
get {
if (_instance == null) {
lock (_singleInstanceLocker) {
if (null == _instance) {
_instance = new SingleInstance();
}
}
}
return _instance;
}
}