Thursday, July 9, 2009

Does making a method static in C# improves its performance?

No, it doesn't improve the method's performance but only substracts a hidden argument from the list.


'static' is a semantic construct, by which you mean that a certain method belongs to the class, or it doesn't need internal data to deal with.





On the other side, if you care about 'calling' performance (again, not the performance of the method itself) then consider not using virtual methods unless really necessary. That should decrease calling overhead (virtual calls go through a vtable) a tiny little bit, which makes sense for functions that are called inside loops or the like.

Does making a method static in C# improves its performance?
Well, yes, if you assume the system does not have to instantiate an object for it to be available.





But performance should not be the reason you make a method static. Remember, a static method can NOT access anything part of an object unless you "pass" the object to the method - and that's why we all got into OO was to stop passing objects...





A static method is a method relating to the CLASS, not a particular object. If the method qualifies, make it STATIC. If it doesn't - don't.


No comments:

Post a Comment