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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Bechir BejaouiPosted Sep 8, 2010, 11:10 PM
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 HobbsPosted Sep 9, 2010, 6:40 PM
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.