Is there some reason why you are using this version and not a simple one that just initializes the instance in the declaration?
public class Singleton { private static Singleton instance = new Singleton(); public static Singleton Instance { get { return instance; } }
This template is short enough that I donโt think itโs a big problem to include it everywhere.
I would advise you to think about whether you really need a lot of singles; they are generally not suitable for testing, etc.
EDIT: If you really want to practice and you are using .NET 4, you can get it with the 6th template, which is on the singleton page of the new home :
public sealed class Singleton { private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }
source share