If you are looking the answer for the Difference between static class and singleton pattern, read this post.This also explains the advantages of singleton pattern over static class and when to use singleton and static class in c#
A static class is a class with static member variables and methods.
A singleton is a design pattern which makes available only a single object of that class.Since it allows a single instance of the class, it is possible to pass this object(instance) as a parameter to methods.
Both Singleton and static class shares variables.
Table of Contents
Difference Between Static Class and Singleton Class
See below the major difference between a Static class and a singleton class
Singleton Class
Singleton pattern is creational design pattern type. Singleton ensures that you create only one instance of a class that will exist throughout your application life time.
In Singleton class,
2) Singletons can implement interfaces and can derive from a base class
3) Singleton class can be passed as a parameter in methods
4) All the principles of object orientation applies to singleton class
5) Singleton class implementation is not so simple as a static class.
- Implementing Registry objects
- SQL Connection Pools
- Thread pools
- While Using Error Logging frameworks such as Log4net
- While Implementing User preferences
- Builder classes, etc.
Static Class
1) No Instance created and methods and members are accessed at the class level directly.
2) Can not Implement interfaces or derive from base class
3) Implementation is simple
4) No object creation and hence can not be passed as a method parameter.
Static classes are very easy to implement than a singleton.The choice of selection should purely be based on the requirement.It doesn’t make any meaning to go for a singleton class when it is enough to have a static class instead.
Summary
Here we covered the difference between static class and singleton pattern, the advantages of singleton pattern over static class and when to use singleton and static class in c#.Hope you find this article helpful.Give your valuable feedback in the comment section below.
You may like to read Static Class in C#
Leave a Reply