Singleton Design Pattern C# Example

R M Shahidul Islam Shahed
3 min readSep 8, 2023

The Singleton design pattern is a creational design pattern that restricts the instantiation of a class to ensure that there is only one instance of that class created in the entire application. It provides a global point of access to that instance. This pattern is particularly useful when exactly one object is needed to coordinate actions across the system, such as a configuration manager, thread pool, database connection pool, or logging service.

Ensure a class has only one instance and provide a global point of access to it.

Singleton Design Pattern C# Example

Here’s an example of implementing the Singleton design pattern in C#:

💻 Singleton Class

public class Singleton
{
private static Singleton instance;
private static readonly object lockObject = new();

public static Singleton GetInstance()
{
if (instance == null)
{
lock (lockObject)
{
if (instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}

public void SomeMethod()
{
Console.WriteLine("Singleton method called.");
}
}

💻Program.cs Class: Main Class

using SingletonEXP;

Singleton _SingletonIns01 = Singleton.GetInstance()…

--

--