It looks like you could:
public sealed class Singleton { IEnumerable<string> Values {get; private set;} private Singleton(bool loadDefaults) { if (loadDefaults) Values = new[]{"quick", "brown", "fox"}; else Values = new[]{"another", "set", "of", "values"}; } public static Singleton Instance { get { return Nested.instance; } } public static void Initialize() { Nested.Initialize(); } private class Nested {
Or create one instance to be updated:
public sealed class Singleton { IEnumerable<string> Values {get; private set;} private Singleton() { Values = new[]{"quick", "brown", "fox"}; } public static Singleton Instance { get { return Nested.instance; } } private static object instanceLock = new object(); private static bool isInitialized = false; public static void Initialize() { lock(instanceLock) { if (!isInitialized) { isInitialized = true; Instance.Values = new[]{"another", "set", "of", "values"}; } } } private class Nested {
And the third option, based on your constant comment and deleting the comment of the nested class:
public sealed class Singleton { IEnumerable<string> Values {get; private set;} private Singleton() { Values = new[]{"quick", "brown", "fox"}; } private static Singleton instance; private static object instanceLock = new object(); public static Singleton Instance { get { Initialize(); return instance; } } public static void Initialize() { if (instance == null) { lock(instanceLock) { if (instance == null) instance = new Singleton(); } } } }
Codenaked
source share