If you use the second approach:
public static class Globals{ public static Singleton Instance = new Singleton(); }
There is nothing stopping you from doing:
Singleton anotherInstance = new Singleton();
You also will not have the same lazy initialization as your first version (attempts), plus you use a public field that does not allow you to have the same flexibility in the future if you need to change what happens when the value is retrieved.
Note that .NET 4 provides a potentially better approach to creating singleton:
public class Singleton { private static readonly Lazy<Singleton> instance = new Lazy<Singleton>( ()=> new Singleton()); private Singleton() {} public static Singleton Instance { get { return instance.Value; } } }
This is good because it is completely lazy and completely thread safe, but also simple.
Reed copsey
source share