Skip to content

Instantly share code, notes, and snippets.

@jaywick
Last active December 26, 2015 02:59
Show Gist options
  • Select an option

  • Save jaywick/7082403 to your computer and use it in GitHub Desktop.

Select an option

Save jaywick/7082403 to your computer and use it in GitHub Desktop.
Simple C# singleton pattern snippet
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
}
@jaywick
Copy link
Author

jaywick commented Oct 21, 2013

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment