Static classes and static members

I'm working with a new team of C# programmers, most of my background is in C++.  Because of a tool named Resharper the team makes almost all code static.  (The tool suggest when something can be made static.)
Is this really a good idea?  Even for code that may seldom get executed?  I have found white papers about improved performance but what about overall efficient memory usage.  Stack vs Heap kind of stuff?
wilfordrocksAsked:
Who is Participating?
 
wdosanjosCommented:
Interesting question.  Though static classes provide better performance than regular classes, it's not a good OO practice to abuse them (see link below):


Otherwise, you are correct regarding the performance of static classes vs. regular classes (see quote below).  Instances of any class use heap memory, given that static classes do not need an instance you would in theory save heap memory by using static classes.

A call to a static method generates a call instruction in Microsoft intermediate language (MSIL), whereas a call to an instance method generates a callvirt instruction, which also checks for a null object references. However, most of the time the performance difference between the two is not significant.

From: Static Classes and Static Class Members
http://msdn.microsoft.com/en-us/library/79b3xss3.aspx 

Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap. Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed.

From: new Operator
http://msdn.microsoft.com/en-us/library/fa0ab757.aspx

I hope this helps.
0
 
edemcsCommented:
Yes, use static method when you do not need to instantiate an object in the heap.  If all you need to do is execute a method and not persist the object itself or keep any members populated, the static method is ideal.  The stack is immediately popped where the heap will keep the object unti gargabe collection runs.
0
 
edemcsCommented:
Here is a good reference to help you understand when you want to use it or not as well: http://channel9.msdn.com/Forums/TechOff/252598-When-to-use-Static-vs-Instance-Classes
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.