-1

im pretty new to C# and i was a little confused about the use of static methods over regular methods. From what i understand, the only benefit they offer is that they require no object in order to be called. But if thats the case, wouldn't it just be more convenient not to assign this method to a class and to define it in the main program page. Is there any real benefit to static methods?

Edit: What i mean by "wouldn't it be more convenient not to assign this method to a class" is to not create a seperate class where i can put this new method in. Wouldn't it just be more convenient to keep this method in the main program's class.

  • 3
    all methods have to belong to a class. (unlike say c or c++) – pm100 Jun 06 '22 at 00:18
  • 1
    Can you determine why all methods of the [File](https://learn.microsoft.com/en-us/dotnet/api/system.io.file) class are static, while all methods of the [FileInfo](https://learn.microsoft.com/en-us/dotnet/api/system.io.fileinfo) class are instance methods? – Jimi Jun 06 '22 at 00:23
  • @Jimi The `File` class is used to do operations on a file and no instance data is needed. `FileInfo` class is used to collect data from a file, which is instance data. – Jeroen van Langen Jun 06 '22 at 08:18
  • Does this answer your question? [What's a "static method" in C#?](https://stackoverflow.com/questions/4124102/whats-a-static-method-in-c) – Progman Jun 06 '22 at 09:23
  • @JeroenvanLangen You mean the File class carries over immediate operations that don't need to (or must not) persist states. But, why are you commenting on this :) It's meant for the OP to investigate the reason behind this functionality. – Jimi Jun 06 '22 at 10:14

2 Answers2

2

If you define it in the "main program page" it wouldn't be as easy to use throughout the application, it would only be available from that 1 file.

Imagine you have a static class MyStaticClass, you could then use those functions throughout the application, not only on the main program file, but in any file, etc.

MyStaticClass.MyStaticMethod();

in a standard class you would have to do something like

new MyClass().MyMethod();

in other words the reason for a "static" is that you do not have to "new" an instance of the object.

JBoothUA
  • 3,040
  • 3
  • 17
  • 46
  • If i define the class in the main program page can't i just call on it normally by saying MyMethod() instead of adding this static method to a seperate class and calling it doing MyClass.MyMethod() – Adam Abdalla Jun 06 '22 at 00:30
  • you can from that 1 file right. most applications have many files – JBoothUA Jun 06 '22 at 00:37
  • 1
    Ah you're right haha. Since im new to C# and have been mainly using single file programs, i completely forgot that most programs use multiple. Thanks for the answer! – Adam Abdalla Jun 06 '22 at 01:03
0

In Addition to answer of JBoothUA, I can say if you are writing some common method that doesn't below to your domain class. Each class can use that method (like some conversation logic...) should be static.

Whenever you are calling method with new classname.methodname each and every time new instance of class created. That occupying disk memory.

While calling static method didn't require any more space

Darshit Gandhi
  • 121
  • 1
  • 7