Mid GoF 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.

Follow:

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