Is Lazy<T> a good solution for a thread safe lazy loaded singleton?
I suggest you to read referencede articles from comments:
In all cases the Lazy<T>
class is thread-safe, but you need to remember that the Value
of this type can be thread-unsafe, and can be corrupted in multithreading environment:
private static Lazy<MyClass> _instance = new Lazy<MyClass>(() => return new MyClass());public static MyClass Instance{ get { return _instance.Value; }}public void MyConsumerMethod(){ lock (Instance) { // this is safe usage Instance.SomeMethod(); } // this can be unsafe operation Instance.SomeMethod();}
Also you can use any constructor you like depending on the environment of your application.