Tutorials Design Patterns in C#

Singleton Pattern — Complete Guide

Singleton Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.

On this page

Design Patterns in C# · Lesson 1 of 69

Singleton Pattern

GoF CoreEnterpriseCloud & Craft

GoF Core · 1 — Create & structure · ~6 min · Module 1: Creational Design Patterns

What is this?

Singleton ensures a type has one shared instance and a global access point. In modern .NET you often prefer DI lifetimes instead of a classic static singleton.

Why should you care?

ShopNest may want one in-process cache coordinator — but a careless singleton becomes a hidden global that ruins tests.

See it live — copy this example

Paste into a C# console or class library project and run dotnet run.

public sealed class ShopNestConfig
{
    private static readonly Lazy<ShopNestConfig> _lazy =
        new(() => new ShopNestConfig());
    public static ShopNestConfig Instance => _lazy.Value;
    public string ConnectionName { get; init; } = "ShopNest";
    private ShopNestConfig() { }
}

Console.WriteLine(ShopNestConfig.Instance.ConnectionName);

Run Example »

Edit the code below and click Run to see the result in Toolliyo’s live editor.

Code
Result

What happened?

  • Lazy creates the instance once in a thread-safe way.
  • The private constructor blocks new ShopNestConfig().
  • Prefer services.AddSingleton() in ASP.NET Core for testability.

Practice next

  1. Create a console app and paste the example.
  2. Print Instance twice and confirm the same values.
  3. Try new ShopNestConfig() and see it fails to compile.
  4. Add a second property and read it from Instance.
  5. Replace with DI AddSingleton and resolve twice.

Remember

One instance, controlled access. Lazy or DI singleton lifetime. Avoid hidden globals when DI works.

ShopNest process-wide feature flags

A small in-memory flag reader registered as singleton.

Outcome: All requests see the same flag snapshot without a static mess.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Singleton Class (ConfigurationManager):?
Short answer: The ConfigurationManager class is designed to ensure that there is only one instance of it throughout the application. Explain a bit more The Instance property handles the lazy initialization of the singlet…
Junior PDF Detailed
What is the Singleton pattern and why is it used?
Short answer: The Singleton pattern ensures a class has only one instance and provides a global point of access to it. It’s commonly used when exactly one object is needed to coordinate actions across the system, such as…
Mid PDF Detailed
Thread-Safe Singleton (Eager Initialization):?
Short answer: The singleton instance is created when the class is loaded, guaranteeing thread safety without locking. However, it may have a slight performance overhead due to the instance being created regardless of whe…
Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Mid PDF Detailed
Redo Functionality:?
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation. Real-world exampl…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details