locked
Advantage of Singleton? RRS feed

  • Question

  • User619554136 posted

    See in below I have writtine a 
                  1. Singleton class Emp1 with Display Method and
                  2. Static Class with Static Method Display. 

    What is the advantage of singleton class over Static Class with Static  members?

    using System;
    
    namespace dp
    {
    	class Program
    	{
    		static void Main(string[] args)
    		{
    			Emp1 e1 = Emp1.GetInstance();
    			Emp1 e2 = Emp1.GetInstance();
    			Console.WriteLine(ReferenceEquals(e1, e2));
    			e1.Display();
    
    			Emp2.Display();
    		}
    	}
    
    	class Emp1
    	{
    		private static readonly Emp1 emp = new Emp1();
    		private Emp1()
    		{
    		
    		}
    
    		public void Display()
    		{
    			
    		}
    
    		public static Emp1 GetInstance()
    		{
    			return emp;
    		}
    	}
    
    	class Emp2
    	{
    		private Emp2()
    		{
    
    		}
    
    		public static void Display()
    		{
    
    		}
    	}
    }


     

    Tuesday, March 30, 2010 9:05 AM

Answers

All replies