Singleton Pattern with C#

Building the Sample

Build the sample and Run, By default it uses Eager Initialization for instantiating the Singleton Object.

Please uncomment the other methods in Create() method under Simulator Class to demo the others.


This sample Demonstrate the multiple ways of writing Singleton Class in C#.

It also demonstrate the Singleton class with Thread Safety.


There are Four methods to create the Singleton Instance :

1. GetInstance() :-  A Normal way which is not thread safe.

2. GetInstanceEager() :- eager initialization of Singleton instance, Thread safety provided by Framework

3. GetInstanceSynchronized() :- Using Synchronized method.

4. GetInstanceVolatile() :- using Volatile instance & lock mechanism.


SINGLETON PATTERN ensures a class has only one instance, and provides a global point of access to it.

1. The Singleton Pattern ensures that you have at most one instance of a class in your application.
2. Provides Global Access point to that instance.
3. Private Constructor +  Static Method or Getter + Static Variable


public sealed class Singleton 
        private static readonly Singleton uniqueInstanceEager = new Singleton(); 
        private Singleton()  
            Console.WriteLine("Singleton Instance Created."); 
      public static Singleton GetInstance() 
          return uniqueInstanceEager ; 

Source Code Files

More Information