What is the advantage and disadvantage of declaring every class method as static in c#

Posted on 2011-10-15
Last Modified: 2012-05-12
Hi Experts,

Can you please let me know what is the advantage and disadvantage of declaring every class methods as static.


public static string GetTitle()

Open in new window

Please suggest the best approach!!
Question by:tia_kamakshi
    LVL 7

    Expert Comment

    What you ask is essentially the same thing as "why use a static class?".  Read this:

    Author Comment

    Can you please give me brief points for advantage and disadvantage like can there can be memory issue if I am going to static class.
    LVL 12

    Expert Comment

    by:Ramkisan Jagtap
    LVL 11

    Accepted Solution

    Pros (of static)
    1. The methods are jit'ed when the type is used for the first time, this make them fast after the first usage.
    2. Static methods and types are supposed to be thread-safe (not guaranteed, "should)" , so you can feel safer making calls to them in multi-threaded applications.
    3. Static variable reduce the memory footprint because there will only ever be one value for the variable in memory no matter how many places it's used.

    Cons (of static)
    1. Cannot be overridden (made virtual or abstract).
    2. Can only used shard data, no instance data can be used in static methods because there will be only one jit'ed version of it.
    3. Because ariables are shared, it's hard to contain changes and keep things thread-safe. If you don't think about this you can create strange application behavior.

    I'm sure there are plenty more, just off the top of my head. Using static is a design decision and should be used on a per case basis.

    If a method does not contain instance level variables, make it static so there is only one jit for it. If you want data shared across all threads and instances, make it static. It's not a good idea to make everything static, you need encapsulation of classes for a good OO design. Generally stick to making helper/utility classes and constants static until you fully understand it's usage.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    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

    18 Experts available now in Live!

    Get 1:1 Help Now