Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Singleton and static classes

Reference for the C# singleton pattern and static classes: thread-safe Lazy<T> singletons, static constructors, when to prefer static classes or dependency injection.

A singleton is a class that has exactly one instance for the whole program, reachable through a well-known access point. Typical uses are a configuration holder, a cache or a registry. In C# you write one with a private constructor and a static property that returns the single instance.

A static class is a different tool: it cannot be instantiated at all, it has only static members, and it is implicitly sealed. Use it for stateless helpers such as Math. If you need an object that implements an interface, can be passed around or replaced in tests, a singleton (or better, a singleton registered in a DI container) is the right choice.

C#
public sealed class Settings
{
    private static readonly Lazy<Settings> instance = new(() => new Settings());
    public static Settings Instance => instance.Value;
    private Settings() { }
}

Try it

C# A thread-safe singleton with Lazy

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Static classes and static constructors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
public sealed class Name
{
    public static Name Instance { get; } = new Name();   // eager
    private Name() { }
}

public sealed class Name
{
    private static readonly Lazy<Name> lazy = new(() => new Name());   // lazy
    public static Name Instance => lazy.Value;
    private Name() { }
}

public static class Helpers { public static int Twice(int x) => x * 2; }
Form Description
Static property initializer The instance is created by the type initializer, which the runtime runs once and thread-safely. Simplest and usually enough.
Lazy<T> The instance is created on first access to Instance. The default mode is thread-safe: only one thread runs the factory and all callers see the same value.
static class Cannot be instantiated, derived from or used as a type argument. May have a static constructor but no instance constructor.
static readonly field A field assigned once in its initializer or a static constructor.

Usage notes

  • Seal the class and make the constructor private. Otherwise a subclass or another new call can create a second instance.
  • Type initializers are thread-safe. The runtime guarantees a static field initializer or static constructor runs once. That is why public static Name Instance { get; } = new(); is already a safe singleton.
  • Timing differs. Without an explicit static constructor, the runtime may run field initializers earlier than first access; with one, it runs just before first use. Lazy<T> makes the moment explicit.
  • Avoid double-checked locking by hand. It is easy to get wrong. Prefer a static initializer or Lazy<T>.
  • Global state hurts testing. Code that calls Settings.Instance directly is hard to test and hides its dependencies. Prefer registering the class as a singleton in a container and injecting it (see Dependency injection).
  • A singleton must be thread-safe itself. One instance shared by many threads needs immutable state or proper locking.
  • Static classes versus singletons. A static class cannot implement interfaces or be mocked; a singleton can. A static class is fine for pure functions with no state.
  • Static state lives for the whole process. It is never garbage collected, so holding large data or event subscriptions in static fields can leak memory.