Last active
August 17, 2017 21:35
-
-
Save aalhour/89e658dda972ec9deb4c to your computer and use it in GitHub Desktop.
A singleton class example in C#.
This file contains 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 sealed class SingletonExample | |
{ | |
// internal singleton instance | |
private static SingletonExample _instance; | |
// lock for thread-safety laziness | |
private static readonly object _mutex = new object(); | |
// private empty constuctor | |
private SingletonExample() | |
{ | |
} | |
// public method, used to obtain an instance of this class | |
// implements double-locking | |
public static SingletonExample Instance | |
{ | |
get | |
{ | |
if (_instance == null) | |
{ | |
lock (_mutex) | |
{ | |
if (_instance == null) | |
{ | |
_instance = new SingletonExample(); | |
} | |
} | |
} | |
return _instance; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so can use
Instance
in any class in the common namespace now?