samedi 10 septembre 2022

Proper singleton class implementation

I started my design patterns learning with a singleton class. Could you explain me the difference between the following Instance property implementations?

public class Singleton
{
    private static Singleton _instance;
    public static Singleton Instance => _instance ?? (_instance = new Singleton());
}
public class Singleton
{
    public static Singleton Instance => _instance.Value;

    private static readonly Lazy<Singleton> _instance =
        new Lazy<Singleton>(() => new Singleton());
}

Aucun commentaire:

Enregistrer un commentaire