Static classes pros and cons [duplicate] - c#

This question already has answers here:
When to use static classes in C# [duplicate]
(11 answers)
Closed 9 years ago.
I am a C++ programmer who makes a switch to C#.I was said not to use static classes.
I understand that in C# if the class has only static members it should be static.(My class has only static members)
Can you please explain me what could be a problem using static classes?
When we really should use them?
Thanks

The main reason is that sometimes (but NOT always), it's better to use a singleton class, because a singleton class can implement interfaces.

Static classes have no difference from non-static classes apart from the fact that you can't instantiate static classes (because they're static of course) and this static class will not be able to take advantage of cool OOP features such as inheritance (it can't be subclassed in C#).
The main thing you have to be aware of when a static class contains static members is making the class static members thread-safe if used by multiple threads.
Can you please explain me what could be a problem using static classes? When we really should use them?
If your class only exposes static members then you can make it a static class, but only if you are sure you will not need instances of this class. In fact, there's absolutely no issues in using static classes, just make then thread-safe (if required) and make sure it fits in with your system's design.

A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated.
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx
Check also this link:
When to use static classes in C#

Related

What are workarounds when we need multiple inheritance in C#? [duplicate]

This question already has answers here:
Multiple Inheritance in C#
(13 answers)
Closed 4 years ago.
All similar questions I've looked though here, mentions multiple interface inheritance. However, I am not sure how MII could be a workaround to the problem.
Lets say, I have two library class (My_Class_1 and My_Class_2) of different methods. Then I want to create a new class, that can use both of these classes' methods natively, like:
public class My_Application : My_Class_1, My_Class_2 {
public My_Application(){
method_from_Class1();
smth_property_declared_in_My_Class_2 = "hello";
}
}
However, that is not possible with C#. What are flexible workarounds, to extend/enrich class with other classes? In PHP, that is unbelievably simple, just in the top of the class we can:
use example_trait_1;
use example_trait_2;
I would rather not use interfaces; in my view, they have no relation to solving this problem.
p.s. I don't want to create initialize objects for those classes. I want them to be native part of the application class.
As others mentioned, it isn't possible (because that's not what inheritance is), but it seems one workaround would be to have a public property of type Class1, inside a class that inherits Class2.
That also gets around the problem of "which class am I looking at now?" since you'd need to explicitly mention the property when you want something of type Class1.
And if you need to modify Class1 first, then you just create a separate class that inherits Class1 first, then have the public property be of that new type.

Can a specific instance of a class be immutable? [duplicate]

This question already has answers here:
Is there an easy way to make an immutable version of a class?
(7 answers)
How do I create an immutable Class?
(8 answers)
Closed 5 years ago.
I want to see if I can make an instance of a class immutable even if the class itself isn't. What I have here is a similar situation to IntPtr.Zero and other static fields like that, except it's a mutable class and not a struct.
class TestClass
{
public static readonly TestClass GoldStandard = new TestClass();
public int field = 0;
}
class Program
{
static void Main(string[] args)
{
TestClass test = TestClass.GoldStandard;
test.field = 1; // I want to keep this from happening.
Console.WriteLine(TestClass.GoldStandard.field);
}
}
What can I do to keep GoldStandard from being modified?
EDIT: I regret that my question was so badly misunderstood. This is clearly not a duplicate of Immutable type and property in C# or How do I create an immutable Class?. Those questions are about how to create an immutable class, and not how to create an immutable instance of a mutable class.
As for Is there an easy way to make an immutable version of a class?, it certainly sounds very similar to my question. However, there are some important reasons it shouldn't count.
The question asks how to make an instance immutable at some point during the instance's lifetime, rather than just having one instance of a mutable class be immutable from the start.
The question asks how an immutable second class can be generated to mimic a mutable class, rather than having one mutable class act as immutable in a specific case.
The answer to the question doesn't even address my question at all, and is mostly about how immutable classes work in general.
I understand moderators can't take too much time to understand the questions in depth when they're scanning for duplicates, but I hardly think it's fair for my question to be closed as a duplicate because of such superficial similarities.

Static fields - if only one instance created from the class [duplicate]

This question already has answers here:
Static vs non-static class members
(7 answers)
Closed 5 years ago.
I have been having a hard time deciding when I should declare a class filed as static.
I got the idea that a static field is shared among all objects created from a class, unlike a regular non-static field, which is held by each of the objects.
Then if it is known that only one object will be created from a class (because I have seen such cases many times), what is the meaning of a static field?
If the definition of "static" is "shared among all instances," does a static field serve the same role as a regular non-static field, provided there is only one object created from the class?
I got this old project from my boss to study C#, and I see some fields declared as static in a class, but there is only one object created from the class, and I am a bit confused what the point is if it does not have multiple instances.
You should not decide to use static just because there´s only one single instance. As you´ve already mentioned there is an instance.
The question if something should be static depends on if your member depends on an internal state, that is do you need to set some property befpore you can incoporate with this member. So even if there´s just one single instance existing this instance shares a set of members defining its state. Only when your member could omit all those information from that single instance it should be marked static.
However making some member static makes many things - e.g. mocking - quite hard as you create a heavy dependency on your class holding that member

Making interface from class [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm having some troubles making an interface for my class. I tried with a simple public void and that worked. But i cannot get it to work with the public static voids in the code below. I think it has something to do with the enum as a parameter in the method. But How do i fix this?
This is the class:
And this is my interface:
Interfaces are contracts. They specify the method signatures for all methods within the contract.
In your interface, you have:
void FFT(/*stuff*/)
Yet, in your implementation, you have defined
static void FFT(/*stuff*/)
Now, why can't we use static? From Joel Spoelsky
Because an interface is a "contract" or an agreement between the consumer (caller) and the provider (callee). An interface describes what and how the calle will provide functionality. There is no need for static members provided by a third party. Static members cannot be overridden by a provider so they do not belong in an interface.
Interface is a contract between caller and callee. Static member belong to class not to the object, so its no point of making method static.
To explain why this really doesn't make sense, the reason to make an interface is so you can pass an object of your class as a reference to the interface type, so the consumer doesn't need to know which underlying type the object is. For example, you might pass an IEnumerable<Foo> to a method that doesn't need to know or care if this is an array or a list or a hashset, etc., it just wants a sequence of Foos.
Static methods are not associated with an instance, so there is no object to reference. There isn't the concept in C# of a static interface which could be used to reference a class rather than an object.
If you had other classes that you wanted to use interchangeably, you could get rid of the static and make these singletons.

Why members in static class are not static by default [duplicate]

This question already has answers here:
Why do members of a static class need to be declared as static? Why isn't it just implicit?
(8 answers)
Closed 7 years ago.
My question is why members are not by default static in static class.
As we see, interface members are by default public and abstract.
Thanks,
Anil
We should ask c# language designer.
But I understand the ratio behind: it forces the programmer to say "this function is static", even if it would be implied by the fact the the class is static.
Maybe It's a matter of readability: when you read a method without body (and no abstract keyword), you know that this method can only be part of an interface.
When you read a method without "static" modifier, you would need to read also class declaration to understand that is part of a static class and therefore static itself

Categories

Resources