Last active
March 28, 2021 07:21
-
-
Save iamralch/9071626 to your computer and use it in GitHub Desktop.
The Singleton Design Pattern in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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