Kiran Mohanty
What is the difference between Singleton and static ?

When we define a class as static, as soon as application is ready, instance of this class gets created by the CLR. Hence

  1. Application will create instance of the class irrespective of consumption of it.

  2. Memory allocation for static object falls under Generation 2. Hence garbage collector does not clean memory even when it’s not used.

  3. Static does not support inheritance.

where as if we go for Singleton design pattern,

  1. Instance gets created in memory when the application requests first time for it’s instance.

  2. As application creates instance on demand, memory performance is better compare to static.

  3. We can inherit singleton class .

By Kiran Mohanty in .NET on Aug 29 2020
  • Prashanth Kundooru
    Sep, 2020 17

    To discuss just the differences:

    1. Static is a keyword while Singleton concept is a design pattern

    2. Fundamental difference is that Singleton gives you an object while static classes proved static methods(methods decorated with static keyword).

    3. Singleton is a creational pattern with only single instance of the class, it can implement interfaces, inherit from other classes and aligns with object oriented concepts. While using static can restrict usage of inheritance and some OOPS concepts.

    4. Also that singleton can be passed as a reference too and supports object disposal.

    5. Also to be noted that static objects can be used part of singleton pattern to help maintain global object. In addition singleton concept can include lazy loading, thread safety and encapsulation.

    Welcome for any new inputs or additions

    • 0
  • Nisha Regil
    Sep, 2020 10

    A singleton allows a class for which there is just one, persistent instance across the lifetime of an application. A Singleton can implement interfaces, inherit from other classes and allow inheritance. A static class allows only static methods and and you cannot pass static class as parameter.

    • 0

Most Popular Job Functions