Static classes and static members

Posted on 2011-05-04
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?
Question by:wilfordrocks
    LVL 8

    Assisted Solution

    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.
    LVL 8

    Assisted Solution

    Here is a good reference to help you understand when you want to use it or not as well:
    LVL 23

    Accepted Solution

    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

    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

    I hope this helps.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Najam
    Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    728 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now