Difference between static class and singleton pattern?

Difference between static class and singleton pattern?

Static Class:-

  1. You cannot create the instance of static class.
  2. Loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
  3. Static Class cannot have constructor.
  4. We cannot pass the static class to method.
  5. We cannot inherit Static class to another Static class in C#.
  6. A class having all static methods.
  7. Better performance (static methods are bonded on compile time)
  8. static objects are stored in stack.
  9. we can not clone the static class object .

Singleton:-

  1. You can create one instance of the object and reuse it.
  2. Singleton instance is created for the first time when the user requested.
  3. Singleton class can have constructor.
  4. You can create the object of singleton class and pass it to method.
  5. Singleton class does not say any restriction of Inheritance.
  6. We can dispose the objects of a singleton class but not of static class.
  7. Methods can be overridden.
  8. Can be lazy loaded when need (static classes are always loaded).
  9. We can implement interface(static class can not implement interface).
  10. Singleton objects are stored in Heap
  11. We can clone (if the designer did not disallow it) the singleton object
  12. Singleton classes follow the OOP (object oriented principles), static classes do not.

Source : https://stackoverflow.com/questions/519520/difference-between-static-class-and-singleton-pattern

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.