In the first version of the implementation in Jon Skeet implementation here, he has the following code:
// Bad code! Do not use!
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
I'm wondering why not just make it:
public sealed class Singleton
{
private Singleton() {}
public static Singleton Instance = new Singleton();
}
What are the differences between the two snippets ?
(I'm aware that using Lazy<T>
is much better solution)