Why the main function is always kept static.
Which will be executed first the static constructor or the main function and why?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Bechir BejaouiRank #144 · 15.9K reputationPosted Sep 8, 2010, 7:10 PM EDT
For the fist question. For C# and Java when an executable is designed then it support only one entry point from where the program is lunched. Now suppose that the Main which is the entry point is none static that means that is instance method that means more than one copy could be operational at the same time which leads to many entry points at the same time thing that is not supported by C# design.
The static main is a method within the class program and then the static constuctor of the class program is lunched before any class member as it will set logic or some set at the lunch time of the first instance of the class. You can see that by adding a static construtor and adding some code within it and then pressing F10 and stepping through the code
Sam HobbsRank #107 · 20.5K reputationPosted Sep 9, 2010, 2:40 PM EDT
Another way to look at it is that the Main function is called by Windows and Windows does not know where this an instance of a class for calling a non-static method.