Static methods vs singletons [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why choose a static class over a singleton implementation?
Static methods vs singletons.
Could it be determined choice here?
What are conditions in which one of these approaches is definetely more suitable then other?

Static methods follow the "low coupling and high coherence"-rule more than singletons do. (As long as their implementations don't rely on static member variables.)

If the static methods solve the same problem as the singleton, then why complicate and write a singleton?
Otherwise, why choose something that solves the wrong problem?

Related

Purpose of static classes [duplicate]

This question already has answers here:
In C#, what is the purpose of marking a class static?
(5 answers)
Closed 4 years ago.
While learning about static classes, I went through many forums as per my understanding I got to know that static class members can be accessed directly using class name and hence not required to create object of that class which in turn helps save memory and faster execution of program.
So my question is if this is the case then why not always use static class over normal class.
I know my question may be little weird, also I may be wrong with the concepts. Please if any one can explain this in detail with example. would be great help.
A static class can not be instantiated. E.g. you can't use new to create an object of that class in C#.
There are many situations where you might want to create multiple objects of a class.

Why can extension methods only be defined in static classes? [duplicate]

This question already has answers here:
Extension methods require declaring class to be static
(1 answer)
Why are extension methods only allowed in non-nested, non-generic static class?
(3 answers)
Closed 9 years ago.
I mean, I'm pretty sure it is a good habit anyway, but are there any technical/conceptual reasons why this is enforced by the compiler? Or is it enforcing aesthetics only?
There are no reasons for this behavior below the C# layer. Starting with the IL layer extension methods and static classes do not exist (except for either unimportant changes or custom attributes).
It is a choice the language designers made, supposedly for code clarity. It does not have to be this way for any fundamental reason.
There are other similar restrictions as well. For example you can't have extensions defined in nested classes.
For my understanding this is a conceptual move.
The static class acts like a pure container for methods and itself does not represents the object, and the extension method by itself is the method that only processes the input and does not acts like a part of the objects behavior.
That is why I think they are very similar by its nature in the C# and probably that is why they where linked together by the language.

In which cases should I use a seal class? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why seal a class?
I haven't seen a lot of classes with this modifier, however I'm wondering in which kind of scenarios this would be useful.
In case you do not wand anyone to extend a class.
Classic example is a String class, it's self-sufficient and implements flyweight pattern, that might be easily destroyed if anyone could overwrite it's methods/properties.
You want to use the sealed class if you provide your API that doesn't need to be changed, for any of above reasons.

Using private static methods Instead of private methods In C# [duplicate]

This question already has answers here:
What is better? Static methods OR Instance methods
(7 answers)
Closed 9 years ago.
When its recommended to use a private static method instead of a private [instance] method?
EDIT:
i am looking for a good (or best practice) about that .i am wondering Is this technic used by microsoft or not?does anybody know something about that ? i coudnt find any blog ,article or sample source code that explains this topic.
any help would be highly appreciated.
Static word in the beginning of the method declaration, basically is a sign of stateless, so what is happening inside is a pure action, or at least should be.
If you want to use private static: use it like API functions of your class that just make some calculations/reports... and not change the actual state of the object, which basically is done by instance methods.
This is an expected way of implementing, which doens't mean that is mandatory, but as it's expected, it will help other developers understand your code, and help understand code to you after a couple of years, when you will come back to your project and already have forgot everything.
Regards.

What are good candidates for singleton? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
On Design Patterns: When to use the Singleton?
Hi
Just wondering what are good candidates for singleton?
Just reading about it and wondering if sometimes I have misused it.
Generally speaking when would you use a singleton?
thanks a lot
Basically, whenever I need a class but am not sure I will need a singleton pattern, I code to the singleton interface, but let the implementing class determine whether or not to return a single instance or a new instance. That way, calling classes don't have to worry about changing if the move to singleton (or the move away from singleton) should occur.
Places I've used this successfully is on my repositories. But I always preface every singleton answer with a 'Make sure you pay attention to thread-safety, whichever way you go'. Sometimes weird things can happen in a multi-threaded environment when dealing with a singleton.

Categories

Resources