Explain C# Singleton class with code example – Simple Steps

(Last Updated On: February 26, 2019)

C# Singleton class is the class that allow only one of its object across the program execution. Singleton class is just a concept and in C# programming any class can be designed as singleton class which is supposed to have only one object of it.

Below are the steps to design a class as a singleton class in C# programming with program example.

Step -1: First we need to make a constructor of the class private so that object of the class cannot be created outside of the class.

Step -2: Class will have a static method for example static GetInstance() that will be responsible to create object of its own and return to the client program. This method will be static as client cannot create object so client can call using class name only.

Step -3: Design singleton class GetInstance() method so it can return only one object on multiple calls.


Singleton class example in C#

This is the complete program for design pattern singleton class in C#. This program has comments and validation of the singleton class objects if they have same address is written.



NOTE: Above program source code for C# singleton class is not designed for multi-threaded environment.