Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static methods vs instance methods in C#

For an application I am writing, I want to have extreme extensibility and extension methods seem to give me what I want, plus the ability to call them without an instance, which I need too.

I remember reading that static methods are faster than instance methods but don't get the advantages of GC. Is this correct?

It's highly unlikely I will change my design unless I find a superior alternative by design not speed. But still for extra information I wanna know the differences in speed, GC, etc.

EDIT: Thanks. More info: Let's say we have a Person class:

class Person

which can have an instance Distance method so like:

this.Distance (Person p)

This is great, but this doesn't give me the ability to calculate the distance between 2 points (say Point3), without creating instances of the Person class.

What I want to do is this:

class Person (no Distance methods)

but extension methods of Distance:

Distance (this Person, Person)
Distance (this Point3, Point3)

This way I can both do:

myPerson.Distance (yourPerson)

and

Extensions.Distance (pointA, pointB)

EDIT2: @Jon, yeah I think that was what was meant by (don't get the advantages of GC), but I somehow thought that the static methods create this burden/overhead.

like image 658
Joan Venge Avatar asked Feb 27 '09 19:02

Joan Venge


2 Answers

Choosing between static and instance methods is a matter of object-oriented design. If the method you are writing is a behavior of a an object, then it should be an instance method. If it doesn't depend on an instance of an object, it should be static.

Basically, static methods belong to a type while instance methods belong to instances of a type.

like image 57
mmx Avatar answered Oct 05 '22 05:10

mmx


What do you mean by "don't get the advantages of GC"? Methods aren't garbage collected - instances are.

Virtual methods are slightly slower than non-virtual ones, and I guess there's that pesky null check before any instance method, but it's not significant. Go with the most appropriate design.

Static methods are a pain for testing though - for instance, if you authenticate in method Foo() by calling some static method, then when you're testing Foo() you can't make it just call a mock authenticator (unless the static method itself lets you do that). If you give the original instance of whatever you're testing a mock implementation of some interface containing an Authenticate() method, however, you can make it behave however you want.

EDIT: In this case, it sounds like what you really need is an instance method on your Point type to calculate the distance between two points ("this" one and another) - or potentially a static factory method on the Distance type.

like image 30
Jon Skeet Avatar answered Oct 05 '22 03:10

Jon Skeet