C# how to create singleton class

4/1/2014·4 min read

This'll show how to cteate singleton class' simple version

Imprement Singleton 1
 class  MySingleton  : Dictionary <int , string >
 	{
 		private  static  object  _locker = new  object ();
 		private  static  Dictionary <int , string > _instance;
 
 		public  static  Dictionary <int , string > Instance
 		{
 			get 
 			{
 				if  (_instance == null )
 				{
 					lock  (_locker)
 					{
 						if  (_instance == null )
 						{
 							_instance = new  MySingleton ();
 						}
 					}
 				}
 
 				return  _instance;
 			}
 		}
 
 		private  MySingleton()
 		{
 			this .Add(0, "name 0" );
 			this .Add(1, "name 1" );
 			this .Add(2, "name 2" );
 			this .Add(3, "name 3" );
 			this .Add(4, "name 4" );
 		}
 	}
 
How to call your Singleton class
 	static  void  Main(string [] args)
 		{
 			Console .WriteLine(MySingleton .Instance[0]);
 
 			Console .Read();
 		}
 
Imprement Singleton 2
 	class  MySingleton2 
 	{
 		private  static  object  _locker = new  object ();
 		private  static  MySingleton2  _instance;
 		private  static  UserInfo  _userinfo;
 
 		public  static  MySingleton2  Instance
 		{
 			get 
 			{
 				if  (_instance == null )
 				{
 					lock  (_locker)
 					{
 						if  (_instance == null )
 						{
 							_instance = new  MySingleton2 ();
 						}
 					}
 				}
 
 				return  _instance;
 			}
 		}
 
 		public  MySingleton2()
 		{
 			// build your data here 
 			_userinfo = new  UserInfo () { Email ="asdf@sdf.com" , FirstName="asd" , Id=1, LastName="asfd"  };
 		}
 
 		public  UserInfo  GetUserInfo()
 		{
 			return  _userinfo;	
 		}
 	}
 

Singleton is very simple to imprement, but it's better not directory calling, it's more safer to do following way.

Create Interface
 	public  interface  IUserInfoService 
 	{
 		UserInfo  GetUserInfo();
 	}
 
return your Singleton's result from imprement class
 	class  UserInfoService  : IUserInfoService 
 	{
 		public  UserInfo  GetUserInfo()
 		{
 			return  MySingleton2 .Instance.GetUserInfo;
 		}
 	}
 

So in this way, at least, when you do your unit-test, you could do Interface Injection to change actual imprementation class to your mock class.

Imprement Singleton 3
  public sealed class Singleton3
     {
         private static volatile Singleton3 instance;
         private static object syncRoot = new Object();
         private static string _getName;
 
         private Singleton3() 
         {
             _getName = "my name";
         }
 
         public static Singleton3 Instance
         {
             get
             {
                 if (instance == null)
                 {
                     lock (syncRoot)
                     {
                         if (instance == null)
                             instance = new Singleton3();
                     }
                 }
 
                 return instance;
             }
         }
 
         public string MyName
         {
             get
             {
                 return _getName;
             }
         }
     }
 

MSDN: Implementing Singleton in C#