Kirtesh Shah
Difference between static and singleton pattern?
By Kirtesh Shah in .NET on Dec 06 2021
  • Pranam Bhat
    Dec, 2021 8

    1) A singleton allows access to a single created instance - that instance (or rather, a reference to that instance) can be passed as a parameter to other methods, and treated as a normal object.

    2) A static class allows only static methods.

    3) static classes are not for anything that needs state. It is useful for putting a bunch of functions together i.e Math (or Utils in projects). So the class name just gives us a clue where we can find the functions and nothing more.

    4) Singleton is used to manage something at a single point. It’s more flexible than static classes and can maintain it’s state. It can implement interfaces, inherit from other classes and allow inheritance.

    5) Singleton Objects stored on heap while static class stored in stack.
    6) Singleton Objects can have constructor while Static Class cannot.
    7) Singleton Objects can dispose but not static class.
    8) Singleton Objects can clone but not with static class.

    9) The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that’s less common, in my experience), so you can pass around the singleton as if it were “just another” implementation.

    Refer this article : https://www.c-sharpcorner.com/UploadFile/akkiraju/singleton-vs-static-classes/

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS