none
Difference between Singleton and Static classes?

    Question

  • IS there any difference between Singleton process and static class usage, if yes the please tell some and please share some singleton code example?

    Thanks


    Thanks!! MCP
    Wednesday, August 26, 2009 6:40 PM

Answers

  • Very different. Singleton class gives you only 1 object at a given time how ever static classes don't give you objects.
    using System;

    public class Singleton
    {
       private static Singleton instance;

       private Singleton() {}

       public static Singleton Instance
       {
          get
          {
             if (instance == null)
             {
                instance = new Singleton();
             }
             return instance;
          }
       }
    }

    TK
    • Marked as answer by GurunGuns Thursday, August 27, 2009 3:56 PM
    Wednesday, August 26, 2009 6:42 PM
  • The Singleton pattern is foremost designed to control object creation.  There's no control if you use a static object, anybody can create a new one and make the reference to the old one disappear if they choose to do so.  A readonly static object however makes an excellent singleton.

    Hans Passant.
    • Marked as answer by GurunGuns Sunday, August 30, 2009 9:49 PM
    Wednesday, August 26, 2009 7:03 PM
  • To add to what Talal said in a different way...A singleton is an actual instantiated object held for the life of the program. Where a true static class is is never instantiated but its static methods are open for use by any operation which might need them.



    William Wegerson (www.OmegaCoder.Com )
    • Marked as answer by GurunGuns Thursday, August 27, 2009 3:57 PM
    Wednesday, August 26, 2009 7:04 PM
  • The Singleton pattern is foremost designed to control object creation.  There's no control if you use a static object, anybody can create a new one and make the reference to the old one disappear if they choose to do so.  A readonly static object however makes an excellent singleton.

    Hans Passant.

    To clarify Hans post, this isn't true if the whole class is a static class. There's no instantiation to the following class:

    public static class SomeHorridGlobalClass
    {
        public static int PoorlyNamedValue { get; set; }
    }

    But it's certainly true in the case where the class isn't static.

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Wednesday, August 26, 2009 7:05 PM

All replies

  • Very different. Singleton class gives you only 1 object at a given time how ever static classes don't give you objects.
    using System;

    public class Singleton
    {
       private static Singleton instance;

       private Singleton() {}

       public static Singleton Instance
       {
          get
          {
             if (instance == null)
             {
                instance = new Singleton();
             }
             return instance;
          }
       }
    }

    TK
    • Marked as answer by GurunGuns Thursday, August 27, 2009 3:56 PM
    Wednesday, August 26, 2009 6:42 PM
  • The Singleton pattern is foremost designed to control object creation.  There's no control if you use a static object, anybody can create a new one and make the reference to the old one disappear if they choose to do so.  A readonly static object however makes an excellent singleton.

    Hans Passant.
    • Marked as answer by GurunGuns Sunday, August 30, 2009 9:49 PM
    Wednesday, August 26, 2009 7:03 PM
  • To add to what Talal said in a different way...A singleton is an actual instantiated object held for the life of the program. Where a true static class is is never instantiated but its static methods are open for use by any operation which might need them.



    William Wegerson (www.OmegaCoder.Com )
    • Marked as answer by GurunGuns Thursday, August 27, 2009 3:57 PM
    Wednesday, August 26, 2009 7:04 PM
  • The Singleton pattern is foremost designed to control object creation.  There's no control if you use a static object, anybody can create a new one and make the reference to the old one disappear if they choose to do so.  A readonly static object however makes an excellent singleton.

    Hans Passant.

    To clarify Hans post, this isn't true if the whole class is a static class. There's no instantiation to the following class:

    public static class SomeHorridGlobalClass
    {
        public static int PoorlyNamedValue { get; set; }
    }

    But it's certainly true in the case where the class isn't static.

    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Wednesday, August 26, 2009 7:05 PM
  • Just to throw in my 2 cents worth... Static classes can have (static) constructors and these are usually called as soon as the assembly hosting the class is loaded, some people refer to that process (correctly or incorrectly) as the class being instantiated.

    The key point here is the one Han's made... you get more control over when/how the singleton classes are created, where as the run time handles the static classes for you. This can help you control load times of your application, by making a class a singleton instead of static if it has heavy work to do when it's constructor runs you can delay running that logic until just before you first need it rather than when the assembly is loaded even if that functionality won't be used in that instance of the hosting process.
    Wednesday, August 26, 2009 7:10 PM
  • Thanks everyone for the reply,
    Hi nobugz & Yort, can you put some more light in your point?
    Thanks!! MCP
    Thursday, August 27, 2009 3:59 PM
  • Hello,

    Just wantd to link a great article Implementing the Singleton Pattern in C# by Jon Skeet.

    Hope it helps.


    Eyal, Regards.

    blog.eyalsh.net
    Thursday, August 27, 2009 4:12 PM
  • But one important advantage in static function in static class is they are fast in execution then its singleton counterpart. In normal applications it did not make any difference but C# in RTOS or embedded situations static class functions perfoms better. (I wrote a sample app with a function that does some string operations, I called it repeatedly from a client app. Static function calls on large looping performed better than singleton object calls)

    Thursday, March 17, 2011 4:51 AM