Created
April 3, 2024 13:57
-
-
Save heiswayi/3b870d286f0b4c56f8af8ec265ff38d7 to your computer and use it in GitHub Desktop.
a simple singleton pattern class using .NET 4's Lazy type
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
using System; | |
namespace SingletonExample | |
{ | |
public sealed class Singleton | |
{ | |
private static volatile Singleton instance; | |
private static object syncRoot = new Object(); | |
private Singleton() {} | |
public static Singleton Instance | |
{ | |
get | |
{ | |
if (instance == null) | |
{ | |
lock (syncRoot) | |
{ | |
if (instance == null) | |
instance = new Singleton(); | |
} | |
} | |
return instance; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment