Mid GoF Patterns

Thread-Safe Singleton (Eager Initialization):?

  • The singleton instance is created when the class is loaded, guaranteeing

thread safety without locking. However, it may have a slight performance

overhead due to the instance being created regardless of whether it is

needed.

public class Singleton

private static readonly Singleton _instance = new Singleton();

private Singleton() { }

public static Singleton Instance => _instance;

More from Design Patterns in C#

All questions for this course