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

Hi Experts,

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

Examples:

public static string GetTitle()
{
}

Open in new window


Please suggest the best approach!!
tia_kamakshiAsked:
Who is Participating?
 
brutaldevConnect With a Mentor Commented:
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.
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
What you ask is essentially the same thing as "why use a static class?".  Read this:

http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
0
 
tia_kamakshiAuthor Commented:
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.
0
 
Ramkisan JagtapLead DeveloperCommented:
0
All Courses

From novice to tech pro — start learning today.