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

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.

Related

Is it possible to a variable/class be accessible to only other specific class? [duplicate]

This question already has answers here:
How to restrict class members to only one other class
(6 answers)
Closed 1 year ago.
Is it possible to have a class that is available just to other specific class, and inaccessible to the "rest" of the code?
Thank you.
For one class, you make nest one class inside another. If you need to later expand this, you can do it by breaking the Solution up among different Projects (dlls), marking a class internal, and using the [InternalVisibleTo] attribute.

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.

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.

Static methods vs singletons [duplicate]

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?

Categories

Resources