C# - Calling a function from static main

后端 未结 2 943
野趣味
野趣味 2021-01-24 09:03

my question is probably very basic but I did not find an answer...

I wrote a function (public checkSomething that gets 2 strings) in program.cs when I tried to call this

相关标签:
2条回答
  • 2021-01-24 09:33

    The question here is not about static functions, but specifically why is main static in a C# application.

    It has to do with having only one thread for the main application. This is defined when using the [STAThread] attribute on the main function. The executing thread enters the main function only once, and no instance of the Program class is required.

    The static main function can then instantiate any classes it requires for normal operation. These are often things like forms, or worker classes.

    The program either enter a message loop in the main thread to process a visible form, or the program terminates.

    When you remove the static reference for main the build options for the project switch from a startup function to a startup object, and an instance of the class is created before main is called.

    It's a matter of personal preference, but keep in mind that only 1 main is ever executed during start up.

    0 讨论(0)
  • 2021-01-24 09:43

    Main method should be static so you can't call non static methods from static method.

    change checkSomething to static.

    if main method is not static then we need to create instance to call the main method. To create instant again we need to excute some code! Thats why I think this starting point Main make Static.

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