none
What is the main difference between static class & Singleton class [duplicate] RRS feed

  • Question

  • here is two piece of class code one is for Singleton and other one is for static class. i like to understand in programming when one should use static class and when one should use Singleton class?

    both are used to hold the global object as a result we can access those data from any where of the program when it is running. scope is broad for both....the life time of application.

    1) i really do not find any article which can guide me when i should use static class and when Singleton class should be good choice. i have seen people manage db connection using Singleton class.

    public sealed class Singleton
    {
        private static Singleton instance = null;
        private static readonly object padlock = new object();
    
        Singleton()
        {
        }
    
        public static Singleton Instance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                    return instance;
                }
            }
        }
    } 
    
    public static class TestStatic //: ITestSingleton
        {
            public static void doAction(string args)
            {
                Console.WriteLine("Test Static :: " + args);
            }
        } 
    Friday, May 9, 2014 2:20 PM

Answers

  • The main difference between a singleton and a static class is that the singleton instance can be passed around (injected) as a parameter to other methods and constructors while a static class only has static methods that can be called without creating an instance of the class.

    Also, a singleton can implement interfaces and derive from a base class while a static class cannot.

    • Marked as answer by Barry Wang Tuesday, May 20, 2014 12:25 PM
    Saturday, May 10, 2014 10:07 AM

All replies

  • What you show as a singleton class is a not a single instance class. Therefore it is hard to say. 

    In fact you are just setting a reference to a static object.

    However, because that C was in fact used on single program multi user computers the name singleton class is still often used for those.

    Avoid to use it in C# some call it currently a singly instance, which can be done by using the mutex.

    However, there is nothing to compare with static. A single instance can be created using a static class while a static class can be thousand times in memory. Therefore apples and pears.

    Be aware that your question has nothing to do with Windows Forms a presentation part in .Net classes and Api's for a desktop.

    Try next time the C# forum for this kind of questions.

    http://social.msdn.microsoft.com/Forums/vstudio/en-US/home?forum=csharpgeneral


    Success
    Cor


    Friday, May 9, 2014 6:11 PM
  • The main difference between a singleton and a static class is that the singleton instance can be passed around (injected) as a parameter to other methods and constructors while a static class only has static methods that can be called without creating an instance of the class.

    Also, a singleton can implement interfaces and derive from a base class while a static class cannot.

    • Marked as answer by Barry Wang Tuesday, May 20, 2014 12:25 PM
    Saturday, May 10, 2014 10:07 AM