What is difference between Singleton and Static class in C#? Example.

Singleton:- allows a class for which there is just one, persistent instance across the lifetime of an application. 

Static class :-allows only static methods and and you cannot pass static class as parameter. 

Singleton :-can implement interfaces, inherit from other classes and allow inheritance.

A- Singleton objects are stored in Heap, but static objects are stored in stack

B-We can clone (if the designer did not disallow it) the singleton object, but we can not clone the static class object.

C-Singleton classes follow the OOP (object oriented principles), static classes do not.

D-We can implement an interface with a Singleton class, but a class's static 
methods (Example:- a C# static class) cannot.


Important  :- Static Classes have only one object life, Hence we cannot create multiple objects of 
static classes but normal classes can be instantiated for creating many objects of the same class.

We use Static classes in scenarios in like creating a utilities class, implementing a singleton pattern.








Share:

No comments:

Post a Comment

Wednesday, 4 September 2019

What is difference between Singleton and Static class in C#? Example.

Singleton:- allows a class for which there is just one, persistent instance across the lifetime of an application. 

Static class :-allows only static methods and and you cannot pass static class as parameter. 

Singleton :-can implement interfaces, inherit from other classes and allow inheritance.

A- Singleton objects are stored in Heap, but static objects are stored in stack

B-We can clone (if the designer did not disallow it) the singleton object, but we can not clone the static class object.

C-Singleton classes follow the OOP (object oriented principles), static classes do not.

D-We can implement an interface with a Singleton class, but a class's static 
methods (Example:- a C# static class) cannot.


Important  :- Static Classes have only one object life, Hence we cannot create multiple objects of 
static classes but normal classes can be instantiated for creating many objects of the same class.

We use Static classes in scenarios in like creating a utilities class, implementing a singleton pattern.








No comments:

Post a Comment

Popular

Total Pageviews

Archive