using System; | |
using System.Collections.Generic; | |
namespace Example.EnsureInitialized | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var configurationService = new ConfigurationService(); | |
var configInfo = configurationService.ConfigInfo(); | |
foreach(var configItem in configInfo) | |
{ | |
Console.WriteLine(configItem.Value); | |
} | |
Console.ReadKey(); | |
} | |
} | |
class ConfigurationService | |
{ | |
private Dictionary<long, string> _simpleExample; | |
public ConfigurationService() | |
{ | |
System.Threading.LazyInitializer.EnsureInitialized(ref _simpleExample, Init); | |
} | |
private Dictionary<long, string> Init() | |
{ | |
// The following would be replaced with some expensive operation like reading a file from disk. | |
return new Dictionary<long, string> | |
{ | |
{1, "Hello"}, | |
{2, "World"} | |
}; | |
} | |
public Dictionary<long, string> ConfigInfo() | |
{ | |
return _simpleExample; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment