Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether to use static class or not [duplicate]

Tags:

c#

.net

c#-4.0

Possible Duplicate:
When to Use Static Classes in C#

I will write code in which I need class which holds methods only. I thought it is good idea to make class static. Some senior programmer argue that do not use static class. I do not find any good reason why not to use static class. Can someone knows in C# language there is any harm in using static class. Can static class usage required more memory than creating object of class? I will clear that my class do not have single field and hence property too.

For further information I will explain code also.

We have product in which we need to done XML handling for chart settings. We read object from XML file in class Library which holds chart related properties. Now I have two Layers first is product second class Library and XML related operations. Actually senior programmers want independent class to read and write XML. I make this class static.

In another situation I have class of chartData. In that class I want methods like whether Line of Axis,series of chart is valid or not. Also whether color of chart stores in ARGB format or plain color name. They do not want those methods in same project. Now can I make class static or create object.

like image 412
Abhijit Shelar Avatar asked Feb 14 '12 05:02

Abhijit Shelar


People also ask

When should you use static classes?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

Why static method should be avoided?

Static methods are bad for testability. Since static methods belong to the class and not a particular instance, mocking them becomes difficult and dangerous. Overriding a static method is not that simple for some languages.

Are static methods more efficient?

They are faster — Static methods are slightly faster than instance methods because in instance methods, you are also working with an implicit this parameter. Eliminating that parameter gives a slight performance boost in most programming languages.

How many copies are kept in memory for a static class?

Only one copy of the static data member exists in the memory. There is no need for a function to explicitly set the value of count because the value of static data members has been initialized to 0 outside the class definition. Now, each object increments the value of count and hence the output.


3 Answers

If your class does not have to manage state then there is absolutely no reason to not declare it static.

In C# some classes even have to be static like the ones that have extension methods.

Now if there's a chance that it requires state in the future, it's better to not declare it as static as if you change it afterwards, the consumers will need to change their code too.

like image 128
vc 74 Avatar answered Nov 03 '22 00:11

vc 74


One concern is that statics can be harder (not impossible) to test in some situations

like image 26
Daniel Powell Avatar answered Nov 03 '22 00:11

Daniel Powell


The danger of static classes is that they often become God Objects. They know too much, they do too much, and they're usually called "Utilities.cs".

Also, just because your class holds methods only doesn't mean that you can't use a regular class, but it depends on what your class does. Does it have any state? Does it persist any data that's being modified in your methods?

like image 35
Jordan Avatar answered Nov 03 '22 00:11

Jordan