Mid From PDF GoF Patterns Gang of Four Patterns

Double-Checked Locking:?

  • This is a more advanced form of lazy initialization that avoids the performance

overhead of locking once the instance has been created.

public 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;
}
}
}

More from Design Patterns in C#

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details