Why is the class “Program” declared as static?

前端 未结 4 1082
后悔当初
后悔当初 2020-12-31 00:14

When you create a WinForm App, you get an automatically generated template of Program class in the Program.cs file.

Which look

4条回答
  •  心在旅途
    2020-12-31 00:53

    The reason for it to be static is, because it only has static methods and nothing else. If you would now add interfaces, base classes and non static methods/properties/members you would have to create an instance of that class to use them (which is forbidden because of the static class). Which is still fine, and could even be done in the static Main method, but it could be misleading or is not the intended purpose of that class. I would create a MyApplication class anway, instance it in the static Main and create my Form from there.

    Regarding your Exception handlers. You can still create a manager class that does that for you, which you just call from your Main and can be reused in all your programs.

提交回复
热议问题