Challenge: Singleton Pattern

Implement the Singleton class then click Validate — your code will be compiled and tested live.

What is a Singleton?
  • Only one instance of the class exists throughout the entire application.
  • The constructor is private — the class cannot be instantiated with new.
  • Access is provided via a static method GetInstance().
  • Ideally thread-safe: if two threads call GetInstance() simultaneously, they get the same instance.
Hints (click to show)
  1. Declare a field private static Singleton? _instance;
  2. In GetInstance(), check whether _instance is null and create it if needed.
  3. For thread-safety, use lock or Lazy<T>.