Skip to content

Instantly share code, notes, and snippets.

@iamralch
Last active March 28, 2021 07:21
Show Gist options
  • Save iamralch/9071626 to your computer and use it in GitHub Desktop.
Save iamralch/9071626 to your computer and use it in GitHub Desktop.
The Singleton Design Pattern in C#
public abstract class Singleton<T> where T : class
{
private static volatile T _instance;
private static readonly object _locker = new object();
public static T Instance
{
get
{
if (_instance == null)
{
lock (_locker)
{
if (_instance == null)
_instance = Activator.CreateInstance(typeof(T), true) as T;
}
}
return _instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment