CalvinR

I have a question about static methods and there overhead, now in a project I'm working on I have to constantly do something now the code where I do this stuff is stored in an object, so I want to know if it would be better to instantiate an instance of this object once and then just call the methods normally or to declare the methods as static and call them that way. Now I would prefer to do it staticaly however I'm unable to find information about how static methods are handled at run time, would my program take a performance hit by doing it statically




Re: Visual C# Language Static method question

Brendan Grant

Static method calls are only slightly faster than instance methods as every time an instance method is called, a null check occurs on the parent object... something that is not needed with a static... although this performance boost is very negligible.




Re: Visual C# Language Static method question

CalvinR

That's great I was worried that there would be a hit in performance not an increase.

Thanks for the timely response.





Re: Visual C# Language Static method question

Sean Hederman

As a rule of thumb, one shoud never worry about performance hits for different ways of calling (virtual vs non-virtual, static vs non-static etc). There may very well be slight performance disadvantages to writing your code the correct way, but these will virtually never add up to anything more than a tiny percentage of the running time of your code. Quite simply, it's not worth bothering about. This kind of worrying is called "premature optimization", and is not considered a good idea.

It's far better to just write your code the way you want to, and then to profile your code for performance once you're finished. This will identify real bottlenecks as opposed to imaginary ones.