Example of Singleton Design Pattern in C#

Share Post
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

Please Subscribe Youtube| Like Facebook | Follow Twitter

In this tip we will Implement Singleton Design Pattern in our code.

public sealed class Singleton
{
 
        private static Singleton obj;       
        public static Singleton Instance() {

            if (obj == null) {

                obj = new Singleton();
            }

            return obj;
        }

      
}

Thread Safe:

public sealed class Singleton
{
        private static Singleton obj;
        private static readonly object padlock = new object();          
        public static Singleton Instance() 
        {
               get  
               {  
                   lock (padlock)  
                   {  
                      if (obj == null) 
                      {
                         obj = new Singleton();
                      }
                      return obj;
                   }
              }
              
        }      
}
Please Subscribe Youtube| Like Facebook | Follow Twitter


Share Post
  •  
  •  
  •  
  • 2
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    2
    Shares

Leave a Reply

Your email address will not be published. Required fields are marked *