C# Static Classes vs Singleton Design Pattern

preview_player
Показать описание
Which do you prefer? I was asked this once in a job interview.
Рекомендации по теме
Комментарии
Автор

I was looking forward to an explanation on the Differences between Static and Singleton class. 
I would say the basic difference being:
Single Class still retains the Oops Concept on Inheritance,  
Singleton class can inherit from Interface, or another class.
and Can be Inherited too. Singleton class can be serialized too.

homerOct
Автор

Awesomely explained. I always find you explaining things better.

parthodave
Автор

"Sorry Jamie" --> Love your personification of the IDE! Wish my IDE would say "Sorry" when showing errors :-)

MatthysduToit
Автор

Having just watched your 22 videos on multi-threading, I was surprised to see you didn't create a single instance of a bathroom stall!

kicknotes
Автор

I would argue that static is preferred if you are creating a stateless service, and use singleton instances when you need to load some external data that could change change between runs of the application. Also, I prefer to use "Logger myLogger = Logger.TheInstance;" so you don't need to type "theInstance" on every line, and can use it with the same minimal amount of code you liked when implementing the static instance.

StevenFarnell
Автор

Second Bigg issue with static is as there is no instance creation there is no way out to dispose static objects untill you close your application . With Singleton class you can implement idisposable and can dispose the instance .
Singleton class provides laziness that is it is kind of ondemand creation of instance for the first time only if somehow instance instance not get disposed .
I never use Singleton more often as from this code smell arises, you have to write all bunch of psuedo code to make your class thread safe, secondly it makes your class untestable, rather I focus on using dependency injection where ever needed.

theghumketu
Автор

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#.
A class having all static methods.
Better performance (static methods are bonded on compile time)

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.
Methods can be overridden.
Can be lazy loaded when need (static classes are always loaded).
We can implement interface(static class can not implement interface).

This is the answer I loved the most - Answered copied from "stackoverflow" post.

nareshravlani
Автор

I just want to say thank you!!! I really was struggling to figure out a problem for a software development job and this makes way more sense. You don't know how much you just helped me!! Thank you :)

callmejobson
Автор

can you please give me real time example where i can use static class?

nishthakapur
Автор

this is the best video talking about singleton, period

vorale
Автор

I have read the document on msdn which lists major difference between static classes and Singleton class which are very few but the only major difference I got is that static classes are not thread safe that is in a multithread application there might be more than one object of a static class, I don't know why can't static classes be thread safe I am still searching for the results.

theghumketu
Автор

So you got the job after that interview? :D

SyedAliRaza
Автор

This is easily your most hilarious video xD

FacePalmProduxtnsFPP
Автор

I'd answer depends on the use of the class I guess... Cant really do dependency injection on a static class due to the ctor but you could inside a static class assign based on a service locator pattern however that's a little more obscure when doing unit tests.Speaking of ctor, if you want a quick way to create a default constructor type ctor and press tab 2x.

rbarone
Автор

Its like I'm listening to a pastor damn you are good

CariagaXIII
Автор

Thank you very much!!! Learning C# now, this helped a lot.

One area where Singleton is preferred is when you want to override default stuff (like ToString(), ). I don't think you can do it with Static classes.

Yea typing is a little pain, but I suppose you can create a variable of Singleton instance once and use it all over! Please correct me if I am wrong :)

raghuZanne
Автор

First, I prefer the right one for the job. :) Singletons allow for easier unit testing for one, so I like them for that. I like the pattern for lazy instantiation too. But static classes are easier to deal with. It all depends on the need I guess. To tell the truth, it's actually quite rare that I use this pattern over a static class. :) I guess for me its about ROI. Great videos BTW. Thanks for all that you do for the software development community!

CodeDreamer
Автор

6:40, what modifier did you press to select like that? Thanks in advance

theZINGcollection
Автор

I wish i could give you more likes on this video good job thumbs up!

MudassarMadni
Автор

What if you wanted to explicitly invoke the destructor. Would you implement a removeInstance() {if theInstance delete(theInstance);}

ryklin