Skip to content

Instantly share code, notes, and snippets.

@sandeepkumar17
Last active June 10, 2022 07:21
Singleton with full lazy instantiation
/// <summary>
/// Thread safe Singleton with full lazy instantiation
/// </summary>
public class Singleton
{
/// <summary>
/// Private Constructor
/// </summary>
private Singleton()
{
}
public static Singleton Instance
{
get
{
return Nested._instance;
}
}
private class Nested
{
/// <summary>
/// Explicit static constructor to tell C# compiler
/// not to mark type as beforefieldinit
/// </summary>
static Nested()
{
}
internal static readonly Singleton _instance = new Singleton();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment