SiteMap  | About    

Singleton Pattern Versus Static Class

In simple words, Singleton is a pattern and not a keyword. The Singleton pattern has several advantages over static classes. A singleton allows a class for which there is just one, persistent instance across the lifetime of an application. That means, it created a single instance and that instance (reference to that instance) can be passed as a parameter to other methods, and treated as a normal object. While a static class allows only static methods and and you cannot pass static class as parameter.

A Singleton can implement interfaces, inherit from other classes and allow inheritance. While a static class cannot inherit their instance members. So Singleton is more flexible than static classes and can maintain state.

C# What is difference between Singleton Pattern and Static class

A Singleton can be initialized lazily or asynchronously and loaded automatically by the .NET Framework CLR (common language runtime) when the program or namespace containing the class is loaded. While a static class is generally initialized when it is first loaded and it will lead to potential class loader issues.

Singleton class follow the Object Oriented Principles, so that singletons can be handled polymorphically without forcing their users to assume that there is only one instance. While static cannot.

Other differencess:

Singleton Objects stored on heap while static class stored in stack. Singleton Objects can have constructor while Static Class cannot. Singleton Objects can dispose but not static class Singleton Objects can clone but not with static class

C# Singleton Pattern Versus Static Class C#

In short, single means single object across the application life cycle, so the scope is at application level. The static does not have any Object pointer, so the scope is at App Domain level. Moreover both should be implemented to be thread-safe. (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.