Singleton Class in C#

A Singleton class in C# is a design pattern that ensures only one instance of a class can be created throughout the application's lifetime. It provides a global point of access to that instance and is commonly used for logging, database connections, and configuration management.

The key principle of the Singleton pattern is to restrict instantiation of a class to a single object by using a private constructor and providing a static method or property to access the instance.

Syntax

Following is the basic syntax for implementing a Singleton class −

public class Singleton {
    private static Singleton instance = null;
    
    private Singleton() {
        // private constructor
    }
    
    public static Singleton GetInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

Key Components of Singleton Pattern

Singleton Class Structure Singleton Class - static Singleton instance - private Singleton() + static GetInstance() : Singleton

  • Private static instance − Holds the single instance of the class.

  • Private constructor − Prevents external instantiation.

  • Public static method − Provides controlled access to the instance.

Using Lazy Initialization Singleton

Example

using System;

class Singleton {
    private static Singleton instance = null;
    private static int counter = 0;
    
    private Singleton() {
        counter++;
        Console.WriteLine("Singleton instance created. Count: " + counter);
    }
    
    public static Singleton GetInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
    
    public void Display() {
        Console.WriteLine("Singleton instance is working!");
    }
}

class Program {
    public static void Main() {
        Console.WriteLine("Attempting to create multiple instances:");
        
        Singleton obj1 = Singleton.GetInstance();
        Singleton obj2 = Singleton.GetInstance();
        Singleton obj3 = Singleton.GetInstance();
        
        obj1.Display();
        
        Console.WriteLine("Are obj1 and obj2 the same instance? " + (obj1 == obj2));
        Console.WriteLine("Are obj2 and obj3 the same instance? " + (obj2 == obj3));
    }
}

The output of the above code is −

Attempting to create multiple instances:
Singleton instance created. Count: 1
Singleton instance is working!
Are obj1 and obj2 the same instance? True
Are obj2 and obj3 the same instance? True

Using Eager Initialization Singleton

Example

using System;

class EagerSingleton {
    private static readonly EagerSingleton instance = new EagerSingleton();
    
    private EagerSingleton() {
        Console.WriteLine("Eager Singleton instance created at class loading");
    }
    
    public static EagerSingleton GetInstance() {
        return instance;
    }
    
    public void ShowMessage() {
        Console.WriteLine("Eager Singleton is ready to use!");
    }
}

class Program {
    public static void Main() {
        Console.WriteLine("Program started");
        
        EagerSingleton obj1 = EagerSingleton.GetInstance();
        EagerSingleton obj2 = EagerSingleton.GetInstance();
        
        obj1.ShowMessage();
        
        Console.WriteLine("Both objects are same: " + (obj1 == obj2));
    }
}

The output of the above code is −

Program started
Eager Singleton instance created at class loading
Eager Singleton is ready to use!
Both objects are same: True

Lazy vs Eager Initialization

Lazy Initialization Eager Initialization
Instance created when first accessed Instance created at class loading time
Saves memory if instance never used Immediate availability, no null checks needed
Requires thread safety in multi-threaded apps Thread-safe by default
Suitable for heavy initialization Suitable for lightweight objects

Common Use Cases

  • Database connections − Single connection pool manager

  • Logging − Single logger instance across the application

  • Configuration settings − Single configuration manager

  • Caching − Single cache manager for the application

Conclusion

The Singleton pattern in C# ensures that only one instance of a class exists throughout the application lifecycle. It uses a private constructor and static access method to control instantiation. Choose between lazy and eager initialization based on your specific requirements for memory usage and thread safety.

Updated on: 2026-03-17T07:04:35+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements