What's a “static method” in C#?

前端 未结 9 2043
再見小時候
再見小時候 2020-11-22 06:48

What does it mean when you add the static keyword to a method?

public static void doSomething(){
   //Well, do something!
}

Can you add the

相关标签:
9条回答
  • 2020-11-22 07:18

    Core of the static keyword that you will have only one copy at RAM of this (method /variable /class ) that's shared for all calling

    0 讨论(0)
  • 2020-11-22 07:23

    When you add a "static" keyword to a method, it means that underlying implementation gives the same result for any instance of the class. Needless to say, result varies with change in the value of parameters

    0 讨论(0)
  • 2020-11-22 07:25

    The static keyword, when applied to a class, tells the compiler to create a single instance of that class. It is not then possible to 'new' one or more instance of the class. All methods in a static class must themselves be declared static.

    It is possible, And often desirable, to have static methods of a non-static class. For example a factory method when creates an instance of another class is often declared static as this means that a particular instance of the class containing the factor method is not required.

    For a good explanation of how, when and where see MSDN

    0 讨论(0)
提交回复
热议问题