Last active
December 26, 2015 02:59
-
-
Save jaywick/7082403 to your computer and use it in GitHub Desktop.
Simple C# singleton pattern snippet
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
| class MyClass | |
| { | |
| #region Singleton Access | |
| private static readonly Lazy<Singleton> instance = new Lazy<Singleton>(() => new Singleton()); | |
| public static Singleton Instance | |
| { | |
| get { return instance.Value; } | |
| } | |
| private Singleton() | |
| { | |
| // initialise | |
| } | |
| #endregion | |
| // rest of class | |
| // note: simply use Singeton.Instance.whatever() to access this class | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated 2014-08-31: Implemented using Lazy<>
Updated 2013-10-23: Fixed #endregion typo on line #L17
Updated 2013-10-21: Surrounded by #region and added note on usage