Possible Duplicate:
Difference between static class and singleton pattern?
Just wanted to know what exactly is the difference between a singleton and static class?
The most important drawback of the singleton pattern is sacrificing transparency for convenience. Consider the earlier example. Over time, you lose track of the objects that access the user object and, more importantly, the objects that modify its properties.
A Singleton class can Dispose, while a static class can not. A Singleton class can have a constructor, while a static class can only have a private static parameterless constructor and cannot have instance constructors. A Static class has better performance since static methods are bonded on compile time.
It is not possible to inherit from a static class, while it is possible with singleton pattern if you want to allow it. So, anyone can inherit from a singleton class, override a method and replace the service. It is not possible to write an extension method to a static class while it is possible for a singleton object.
The singleton, like any other instance of a class, lives on the heap. To its advantage, a huge singleton object can be lazily loaded whenever required by the application. On the other hand, a static class encompasses static methods and statically bound variables at compile time and is allocated on the stack.
In a singleton you can choose to initialize the variable at first call. Whereas a static variable starts to exists at the moment you include / call the file where the static variable is declared.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With