none
Can anyone Explain me the difference between Static class vs Singleton class RRS feed

  • Question

  • Hi,Can anyone Explain me the difference between Static class vs Singleton class,i am expecting the specific use cases with an example.

    "Singleton Objects stored on heap while static class stored in stack" this is true or not??

    Regards,

    Latheesh


    Latheesh K Contact No:+91-9747369936


    • Edited by La07K Wednesday, September 14, 2016 11:38 AM
    Wednesday, September 14, 2016 11:37 AM

Answers

  • Hi Latheesh K,

    Thank you for posting here.

    >>Can anyone Explain me the difference between Static class vs Singleton class?

    1. A singleton classes allowed to create a only single instance or particular class. That instance can be treated as normal object. You can pass that object to a method as parameter or you can call the class method with that Singleton object. While static class can have only static methods and you can not pass static class as parameter.
    2. We can implement the interfaces with the Singleton class while we can not implement the interfaces with static classes.
    3. We can clone the object of Singleton classes we can not clone the object of static classes.
    4. Singleton objects stored on heap while static class stored in stack.
    5. A Singleton class can extend the classes(support inheritance) while static class can not inherit classes.
    6. Singleton class can initialize lazy way while static class initialize when it loaded first
    7. Static classes are sealed class while Single ton classes are not sealed.

    >>i am expecting the specific use cases with an example.

    The following implementation of the Singleton design pattern follows the solution presented in Design Patterns: Elements of Reusable Object-Oriented Software [Gamma95] but modifies it to take advantage of language features available in C#, such as properties:

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

    For more details about singleton, please refer to the link.

    >>"Singleton Objects stored on heap while static class stored in stack" this is true or not??

    Yes, it is true.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by La07K Thursday, September 15, 2016 7:50 AM
    Thursday, September 15, 2016 7:38 AM
    Moderator

All replies

  • Hi Latheesh K,

    Thank you for posting here.

    >>Can anyone Explain me the difference between Static class vs Singleton class?

    1. A singleton classes allowed to create a only single instance or particular class. That instance can be treated as normal object. You can pass that object to a method as parameter or you can call the class method with that Singleton object. While static class can have only static methods and you can not pass static class as parameter.
    2. We can implement the interfaces with the Singleton class while we can not implement the interfaces with static classes.
    3. We can clone the object of Singleton classes we can not clone the object of static classes.
    4. Singleton objects stored on heap while static class stored in stack.
    5. A Singleton class can extend the classes(support inheritance) while static class can not inherit classes.
    6. Singleton class can initialize lazy way while static class initialize when it loaded first
    7. Static classes are sealed class while Single ton classes are not sealed.

    >>i am expecting the specific use cases with an example.

    The following implementation of the Singleton design pattern follows the solution presented in Design Patterns: Elements of Reusable Object-Oriented Software [Gamma95] but modifies it to take advantage of language features available in C#, such as properties:

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

    For more details about singleton, please refer to the link.

    >>"Singleton Objects stored on heap while static class stored in stack" this is true or not??

    Yes, it is true.

    I hope this would be helpful to you.

    If you have something else, please feel free to contact us.

    Best Regards,

    Wendy


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by La07K Thursday, September 15, 2016 7:50 AM
    Thursday, September 15, 2016 7:38 AM
    Moderator
  • Thanks Wendy Zang...)

    Latheesh K Contact No:+91-9747369936

    Thursday, September 15, 2016 7:51 AM
  • One small doubt as i am having lesser experience...

    "Singleton objects stored on heap while static class stored in stack"

    my question is what benefit we are getting weather object stored in heap or stack? any performance benefit??

    Can u explain to me....


    Latheesh K Contact No:+91-9747369936

    Thursday, September 15, 2016 7:56 AM
  • Hi,

    great reply, but the last part is wrong in my eyes. Static classes are not stored on the stack from my understanding.

    Instead they are located inside the Loader Heap. A nice description can be found at Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects.

    With kind regards,

    Konrad

    Thursday, September 15, 2016 7:57 AM
  • Thanks Konrad...

    Latheesh K Contact No:+91-9747369936

    Thursday, September 15, 2016 8:02 AM