3
    static int _i;
    static void Display()
    {
        //operates on _i;
    }

AND

    static int _i;
    void Display()
    {
        //operates on _i;
    }

Which are the scenarios where being specific about method's static-nonstatic-ness matter?

Edit; Note: This question is not about differences between static and non static methods as many seem to answer. The question is what are the scenarios/use-cases where I should be worrying about the differences. For clarity see @ziesemer's answer which seem to exactly address it.

nawfal
  • 70,104
  • 56
  • 326
  • 368

3 Answers3

6

There is a tiny difference, the instance (non-static) version is implemented with a 'hidden' parameter called this. Since it won't be used to access static fields it is slightly more economic to use a static method here.

The general recommendation is: If a method can be static then make it static. But that is more about showing clear intent than about performance.

H H
  • 263,252
  • 30
  • 330
  • 514
2

Non-static functions would allow you to create a subclass of the class containing the functions, providing overridden methods for one or more of the methods - providing an option for extensibility and customization. This won't work with non-static functions. On the other hand, non-static functions require you to first create an instance of the class before calling its functions.

I'd also revisit why you have static variables for everything, and encourage you to revisit - instead making everything non-static. This will allow you to have multiple, independent instance of everything within the same runtime. By making your variables static, you are essentially forcing everyone to use the same configuration (or whatever your variables store) - even if they create multiple instances of the containing class.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
  • 2
    Static functions are also more performant, requiring fewer lookups to find them. However, that performance gain almost never matters. – Matt Greer Dec 27 '11 at 23:04
  • @ziesmer the first part is a nice answer. I'm aware of the second part, but sometime or the other we need to have static fields for a class aint it.. – nawfal Dec 27 '11 at 23:37
0

When a non-static method is called on a class, it is given the object in question as an invisible first parameter "this", passed by value; when a non-static method is called on a struct, it is given an invisible first parameter "this", passed by reference. The parameter will be passed, whether or not the function actually makes use of it, because the caller will in many cases have no way of knowing whether the function will actually use the parameter. The function thus has to expect that a caller will pass in "this", unaware that it won't be used, and thus if the function doesn't use "this" it must ignore the first passed-in parameter. Thus, even if the caller knew the function wouldn't use "this", it would have to pass in an extra parameter for the function to ignore.

supercat
  • 77,689
  • 9
  • 166
  • 211