DEV Community

Cover image for Singleton Design Pattern
Tushar Koshti
Tushar Koshti

Posted on • Updated on • Originally published at 07101994.github.io

Singleton Design Pattern

Singleton design pattern makes sure that there should be only one instance of the specific class at a given time. Please refer to the example code for C# implementation.

Example

// Singleton class Customer
public class Customer
{
    private static readonly Lazy<Customer> _lazy = new Lazy<Customer>(() => new Customer());

    private Customer() { }

    public static Customer Instance
    {
        get
        {
            return _lazy.Value;
        }
    }
}

Top comments (0)