Scan with Phone

Scan to instantly open and share this page on your mobile device.

Link copied to clipboard!

Singleton

Goal: exactly one instance. |instances(C)| = 1.

C#

public sealed class Log
{
    private static readonly Lazy<Log> _inst = new(() => new Log());
    public static Log Instance => _inst.Value; // ensures single instance
    private Log() {} // block external
    public void Write(string msg)
    {
        Console.WriteLine(msg);
    }
}
// usage
Log.Instance.Write("hello");
next