[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Static classes and static members

Posted on 2011-05-04
3
Medium Priority
?
287 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:wilfordrocks
  • 2
3 Comments
 
LVL 8

Assisted Solution

by:edemcs
edemcs earned 800 total points
ID: 35690664
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
 
LVL 8

Assisted Solution

by:edemcs
edemcs earned 800 total points
ID: 35690681
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
 
LVL 23

Accepted Solution

by:
wdosanjos earned 1200 total points
ID: 35690770
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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Loops Section Overview
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question