Mid
From PDF
Power Questions
High-Impact Interview Questions
Thread-Safe Singleton?
Short answer: Incorrect (not thread-safe) public class Singleton
Example code
{
private static Singleton _instance;
} Correct using double-check locking public sealed class Singleton
{
private static Singleton _instance;
private static readonly object _lock = new object();
private Singleton() {}
public static Singleton Instance
{ get {
if (_instance == null)
{ lock(_lock) {
if (_instance == null)
_instance = new Singleton();
}
}
return _instance;
}
}
} Best and simplest public sealed class Singleton
{
public static readonly Singleton Instance = new Singleton();
private Singleton(){}
}
Real-world example (ShopNest)
Answer with: definition → one ShopNest-style story → trade-off → how you would verify it in production.
Say this in the interview
- Define — one clear sentence (the short answer above).
- Example — relate it to a project like ShopNest or your real work.
- Trade-off — when you would not use it.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png