Wednesday, June 10, 2009

static Vs instance methods

Perfomance , instance methods are not virtual by default but can be declared as virtual. Virtual method has slightly high overhead than a non virtual method. Static methods are not virtual.
So there might be a slight performance issue and this can be ignored as it's not make much impact on performance.

Choosing your method to be static or instance will depend upon the design. If the method going to be used as an object then it should be an instance method. If it doesn't depend on an instance of an object, it should be static. Static methods belong to a type and instance methods belong to instance of a type.

example

Public Class Car
{
// this is a static method
public static void staticMethod()
{
//your code
}

// this is an instance method
public void instanceMethod()
{
//your code
}
}


Accessing static method
Car.staticMethod();

Accessing instance method
Car car = new Car();
car.instanceMethod();

1 comments:

Kaminda Berugoda said...

Good work mate!! Keep it up!

My Achievements

Member of

Blog Archive

Followers

free counters