Singleton vs Static Class

Published on Saturday, January 4, 2014

Singleton pattern, is actually a design pattern that was from Microsoft patterns & practices. And, when we say Singleton, we should know it is a class. About more details, we can read msdn.

From msdn page we can get to know: When we need a object to be available to all other objects in our application, but we want this object is the only one in whole system (Note: we do not mean the object is a variable only ). We might have different ways to define a global object in scope, such as in C++, but we can not force the global object be created only one instance.

So we need a solution to resolve above problem, this is why we have Singleton. Singleton ensures that only a single instance of a class can exist.

Static Class is for putting multiple static function (method) together. It is still a global object.

Singleton is actually has the feature like regular class, so you can inherited a Singleton from an interface, thus the Singleton class can implement a interface:

 public class MySingleton : IMyInterface  

But Static Class can not inherited from an interface, also, it can not be instantiated, actually we do not need to instantiate it but just use it directly.

Singleton is stored in Heap, static class is stored in stack;

There is another good point from here, a Microsoft employee gave good explanation (see here) :

A “singleton” can be viewed as a shared instance of a class. This does not necessarily preclude there being other instances.* For example, System.Text.Encoding.Unicode is a singleton, but you can create instances of Unicode that are configured differently (e.g., you can change how it deals with errors).