Home All Groups Group Topic Archive Search About

Advantages of Static Vs Instance methods?

Author
13 Jan 2006 10:01 PM
Diffident
Hello All,

Can anyone please list me the advantages or disadvantages of instance and
static methods in terms of performance be it either memory consumption or CPU
utilization or any other parameters.

Thank you.

Author
13 Jan 2006 10:15 PM
Josh Twist
Wow, interesting question... if kind of odd.

Performance wise two identical methods implemented as static and
instance would have very (very) little difference in terms of
performance. Certainly nothing to worry about!

The choice of static vs instance methods is typically more about
design. FxCop views the matter fairly simplistically - if your method
doesn't touch any members of the class then it should be static.

As a rule of thumb though, one isn't any faster then the other.

Josh
http://www.thejoyofcode.com/
Author
13 Jan 2006 10:41 PM
Diffident
Josh,

You said that instance and static have very little difference in terms of
performance. I am curious to know what that difference is and why?

Thank you.

Show quote
"Josh Twist" wrote:

> Wow, interesting question... if kind of odd.
>
> Performance wise two identical methods implemented as static and
> instance would have very (very) little difference in terms of
> performance. Certainly nothing to worry about!
>
> The choice of static vs instance methods is typically more about
> design. FxCop views the matter fairly simplistically - if your method
> doesn't touch any members of the class then it should be static.
>
> As a rule of thumb though, one isn't any faster then the other.
>
> Josh
> http://www.thejoyofcode.com/
>
>
Author
15 Jan 2006 3:12 PM
Josh Twist
A static method can be called without an instance of the class being
created. e.g.

public class Example
{
  public static void Method1()
  {
    // this is a static method
  }

  public void Method2()
  {
    // this is an instance method
  }
}

Method1 is a static method of the Example class and is called like
this:
Example.Method1()

Method2 is an instance method of the Example class and is called on an
instance of an Example class:
Example myInstance = new Example();
myInstance();

At the end of the day they're both methods and have no real difference
in terms of performance. Which one to use is a question of design. I
suggest you have a bit of a read up on OOP (Object Oriented
Programming) to help you make these decisions

Josh
http://www.thejoyofcode.com/
Author
15 Jan 2006 3:13 PM
Josh Twist
Oops! the instance method should be called like this:

Example myInstance = new Example();
myInstance.Method2();

NOT!!! Example myInstance = new Example();
myInstance();

Sorry

AddThis Social Bookmark Button