/st_full-lazy-instantiation.cs Secret
Last active
June 10, 2022 07:21
Singleton with full lazy instantiation
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
/// <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