public sealed class Singleton {
// Private Constructor
Singleton( ) { }
// Private object instantiated with private constructor
static readonly Singleton instance = new Singleton( );
// Public static property to get the object
public static Singleton UniqueInstance {
get { return instance;}
}
}
To create an object, the client calls the UniqueInstance property as in:
Singleton s1 = Singleton.UniqueInstance;
Following two instantiations will refer to the same object:
Singleton s1 = Singleton.Instance;
Singleton s2 = Singleton.Instance;
// Private Constructor
Singleton( ) { }
// Private object instantiated with private constructor
static readonly Singleton instance = new Singleton( );
// Public static property to get the object
public static Singleton UniqueInstance {
get { return instance;}
}
}
To create an object, the client calls the UniqueInstance property as in:
Singleton s1 = Singleton.UniqueInstance;
Following two instantiations will refer to the same object:
Singleton s1 = Singleton.Instance;
Singleton s2 = Singleton.Instance;