Skip to content

Instantly share code, notes, and snippets.

@sunmeat
Last active January 15, 2026 22:03
Show Gist options
  • Select an option

  • Save sunmeat/cd4089b8a3812804d39317251fad7a91 to your computer and use it in GitHub Desktop.

Select an option

Save sunmeat/cd4089b8a3812804d39317251fad7a91 to your computer and use it in GitHub Desktop.
singleton classic C# example
namespace SingletonExample
{
class Program
{
static void Main()
{
Singleton obj = Singleton.GetInstance();
Console.WriteLine(obj.GetValue());
obj.SetValue(75);
Console.WriteLine(obj.GetValue());
}
}
public class Singleton
{
private static Singleton? instance = null;
private int value;
private Singleton()
{
value = 50;
}
public static Singleton GetInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
public int GetValue()
{
return value;
}
public void SetValue(int value)
{
this.value = value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment