Static classes and classes with static methods

6

When I declare a class static , I am required to write my static methods and this class can never be instantiated. Now, I have a common class and create my methods inside it all static. Well, I even consider this static class, for consideration only by its methods, but is it still a normal class or not? When I say normal, I mean not static .

    
asked by pnet 27.02.2015 в 15:22

2 answers

6

Without the explicit declaration of the class as static it is a normal class and can be instantiated unless you prevent it ( see how in that question ). So it has different implications to have the static modifier in addition to the documentation that can only have static members.

But one must ask why creating a normal class when all its members are static. Is there any reason? If you do not find it, you are doing something "wrong."

Remember that all members need to be static, not just methods.

In C # 6 you can import static classes, not normal classes, with:

using static System.Console;
using static System.Math;

lets you use:

WriteLine(Sin(12));

If these classes were not static, you could not do this. Just to cite an example.

    
27.02.2015 / 15:37
2

It remains a non-static class because you can instantiate it.

Static methods are identical across all instances of class objects.

Static attributes are identical across all instances of objects in the class, such as methods. A common use of static attributes is the object count of that class.

    
27.02.2015 в 15:28