Skip to content

Instantly share code, notes, and snippets.

@heiswayi
Created April 3, 2024 13:57
Show Gist options
  • Save heiswayi/3b870d286f0b4c56f8af8ec265ff38d7 to your computer and use it in GitHub Desktop.
Save heiswayi/3b870d286f0b4c56f8af8ec265ff38d7 to your computer and use it in GitHub Desktop.
a simple singleton pattern class using .NET 4's Lazy type
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