C# entry point function

前端 未结 6 638
闹比i
闹比i 2020-11-29 12:55

Is staic void main() is necessary for entry point function in C# or we can use some other functions? Why main() is static?

6条回答
  •  醉话见心
    2020-11-29 13:38

    static int Main(string[] args) is static because it reduces the number of potential steps the runtime needs to go through before it can start executing the entry point of your program (run class's static constructor, run class's static Main... versus run class's static constructor, instantiate the class into some location that's undefined, run instance's constructor, then run instance's Main method). You'd have to write boilerplate code to store this into an instance variable you could use (and likely to keep it from going out of referential scope which would enable it to be garbage-collected) which would also tend to increase the reference count and require a little more memory and pricessing for not much gain.

    It's named Main because C# is strongly reminiscent of Java in its design, and Java uses the name Main, which itself derives from a slight differentiation (case is a thing!) from C and C++.

提交回复
热议问题