Articles → .NET DESIGN PATTERN → Singleton Design Pattern In C#

Singleton Design Pattern in C#






Purpose





Implementation




  1. Make the constructor of the class private
  2. Create a method that returns the instance of the class


public class SingletonClass 
{
        static SingletonClass _singletonClass;
        private SingletonClass()
        {
 
        }
        public static SingletonClass GetInstance()
        {
                if (_singletonClass == null)
                        _singletonClass = new SingletonClass();
 
                return _singletonClass;
        }
}





Calling The Singleton Class




SingletonClass singletonClass = SingletonClass.GetInstance(); 



Posted By  -  Karan Gupta
 
Posted On  -  Sunday, October 10, 2010
 
Updated On  -  Tuesday, February 6, 2024

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250