1

While not positive, I'm pretty sure the static keyword makes methods and fields belong to a class and not an instance of a class. For fields, this makes sense to me, as static fields become global variables essentially. For methods though, I don't understand why it would be advantageous or hurtful to make a static method.

For example, what would be the difference between:

class RandomClass {
  public static void Method() {
    Console.WriteLine("Hello World");
 }
}
class Program {
 static void Main(string[] args) {
  RandomClass.Method();
 } 
}

and

class RandomClass {
  public void Method() {
    Console.WriteLine("Hello World");
 }
}
class Program {
 static void Main(string[] args) {
  RandomClass randomObject = new RandomClass();
  randomObject.Method();
 } 
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Flash
  • 19
  • 4
  • If the method of class deals with fields or property of class, static methods can not be used. – Chetan Jun 28 '20 at 06:18
  • 2
    Without `static` you force the user to create an object in order to call the function, when no object is actually needed. You also create the (misleading) appearance that the outcome of the method depends somehow on which object calls it. – dxiv Jun 28 '20 at 06:19
  • 6
    If you understand the need for global variables, then you should probably also understand the need for global functions. And those are very similar to static methods. It's very convenient to just be able to call `Math.Cos` without having to create a `Math` object first, isn't it? – Sweeper Jun 28 '20 at 06:20
  • @Sweeper that makes sense, but why would I ever make it not static if static is so much more convenient? – Flash Jun 28 '20 at 06:23
  • @ChetanRanpariya would you be able to elaborate on that? – Flash Jun 28 '20 at 06:25
  • @dxiv My apologies, that was not my intent nor what I thought. In that case, why wouldn't I always use static? – Flash Jun 28 '20 at 06:26
  • 1
    @Flash You *cannot* make a method static if it uses/references any other non-static members of the class. In that case you have no choice but to make it non-static. So the only question remaining is what to do about methods that *can* be static, which is precisely what the question I linked is about. – dxiv Jun 28 '20 at 06:28
  • 2
    Technically, you are right. You can try doing that in all your code from now on :) You'll soon find this interesting pattern: some of your methods will take an instance of the enclosing class because you want it to access the class' instance variables, which a static method normally can't do. These methods should be rewritten as non-static methods. – Sweeper Jun 28 '20 at 06:29
  • @dxiv Thanks for the link! Would I be correct in summing it up as static slightly increases performance and convenience, but for extremely large projects it's simply not worth the hassle? – Flash Jun 28 '20 at 06:31
  • 1
    @Flash I wouldn't put it that way. You may want to read the entire discussion, answers and comments. – dxiv Jun 28 '20 at 06:34
  • @dxiv I've read a couple more answers. Would you consider a more accurate summary to be static gives some performance and convenience, but I should first think carefully of what I plan to do with the method down the line? – Flash Jun 28 '20 at 06:40
  • 1
    @Flash I think static vs non-static is more about how the method interacts with the class or instances of the class. If you don't need to use a specific instance, then a static method is the way to go. This has a little to do with "convenience" but I don't think that's the right way to think about it and performance shouldn't factor into the decision at all really. – Code-Apprentice Jun 28 '20 at 06:44
  • @Code-Apprentice Perfect, thank you. – Flash Jun 28 '20 at 06:45
  • 1
    @Flash No, the main point is that `static` conveys (a lot) more accurately the intent of the method. Exposing a method as non-static which does in fact not require or use any particular object instance is a misrepresentation of what the method is meant to do. – dxiv Jun 28 '20 at 06:48
  • 1
    @Code-Apprentice '*performance shouldn't factor into the decision*" Some objects are expensive to create. Requiring one when it's not needed is poor design. Suppose for example that I write a Window class with a static GetWindowsTheme() method. Then when I start the app I want to save the theme information to a logfile. Making GetWindowsTheme() non-static would require the logging code to (unnecessarily) create a (bogus) window just to obtain the system-wide theme information. I see no good reason to justify that. – dxiv Jun 28 '20 at 06:52
  • 1
    Static methods cause problems when using dependency injection and unit testing - you can't mock them up. – Alexander Petrov Jun 28 '20 at 06:58

1 Answers1

2

A static method in C# is a method that keeps only one copy of the method at the Type level, not the object level. That means, all instances of the class share the same copy of the method and its data. The last updated value of the method is shared among all objects of that Type.

Reference: https://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/static-methods-in-C-Sharp/#:~:text=A%20static%20method%20in%20C%23,all%20objects%20of%20that%20Type.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197