What is the difference between static class and singleton?
Murali Poola
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. We can implement the interfaces with the Singleton class while we can not implement the interfaces with static classes. We can clone the object of Singleton classes we can not clone the object of static classes. Singleton objects stored on heap while static class stored in stack. A Singleton class can extend the classes(support inheritance) while static class can not inherit classes. Singleton class can initialize lazy way while static class initialize when it loaded first Static classes are sealed class while Single ton classes are not sealed.
Static class and singleton method -:Static class you need to create a class which will operate to you like API base, so basically it's just functions or constants set. For the user of your class it's being static signals about the lack of the state, basically. Singleton is just a class whom instance can be only one, and the case that you provide static property and private default constructor (cause that what you should do for not letting to your class user create objects) it's just consequence of design. The Singleton pattern is foremost designed to control object creation. There's no control if you use a static object, anybody can create a new one and make the reference to the old one disappear if they choose to do so. A readonly static object however makes an excellent singleton. Static Class - You can not create the instance of static class. Singleton pattern - you can create one instance of the object and reuse it. Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded. Singleton instance is created for the first time when the user requested. Static Class class cannot have constructor. singleton class can have constructor. Singleton class does not say any restriction of Inheritence.So we should be able to do this as long as subclass is also inheritance.There's nothing fundamentally wrong with subclassing a class that is intended to be a singleton. but We can not inherit Static class to another Static class in C#. after all singleton is kind of design pattern Singletons allow you to reuse code and control object state much easier. This improves code-sharing, and can result in a far cleaner body of code. With less code, your programs will usually have fewer bugs and will be easier to maintain.
Static classes and singletons both provide sharing of redundant objects in memory, but they are very different in usage and implementation. Static Class:
You cannot create the instance of static class.
Loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Static Class cannot have constructor.
we cannot pass the static class to method
We cannot inherit Static class to another Static class in C#.
Singleton:
You can create one instance of the object and reuse it. Singleton instance is created for the first time when the user requested. singleton class can have constructor.
you can create the object of singleton class and pass it to method
Singleton class does not say any restriction of Inheritance.
We can dispose the objects of a singleton class but not of static class