为什么大多数 C# 程序中的 main 方法入口点是静态的?
为什么大多数 C# 程序中的 main 方法入口点是静态的?
Why is the main method entry point in most C# programs static?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
为什么大多数 C# 程序中的 main 方法入口点是静态的?
Why is the main method entry point in most C# programs static?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
为了调用实例方法,您需要一个对象的实例。这意味着为了启动您的程序,CLR 需要创建一个
Program
实例,以便调用Main
方法。因此,Program
的构造函数将在Main
之前运行,这完全违背了拥有 main 的目的。In order to call an instance method you need an instance of an object. This means in order to start your program the CLR would need to create an instance of say
Program
in order to call the methodMain
. Hence the constructor ofProgram
would run beforeMain
which defeats the purpose of having a main altogether.我想把问题转过来。实现允许 Main 成为实例方法的功能有什么引人注目的好处?功能价格昂贵;如果没有令人信服的好处,它们就不会得到实施。
您是否有充分的理由为什么应该允许 Main 成为实例方法?
I'd turn the question around. What is the compelling benefit of implementing the feature that allows Main to be an instance method? Features are expensive; if there is no compelling benefit, they don't get implemented.
Do you have a really good reason why Main should be allowed to be an instance method?
从概念上讲,您只有一个静态实例。静态方法很好地映射到程序的单个起始点的习惯用法。语言设计者本可以创建一个特殊的程序类来与 main 方法一起使用,但选择创建一个静态函数作为入口点。在某些层面上,这实际上只是一种设计选择。
Conceptually you only have one instance of a static. And a static method maps well to the idiom of a single staring point for a program. The language designers could have created a special program class to use with a main method but chose to create a single static function as the entry point. On some levels its really just a design choice.
因为否则它必须创建一个对象,并且运行构造函数可能会导致负面影响。
Because otherwise it would have to create an object, and running the constructor could cause negative side effects.
否则你怎么能在 main 之前创建你的类实例呢?
How could you create your class instance before main otherwise?
.NET 运行时
调用Main
方法。 (注意:Main 也可以从其他地方调用,例如从ExampleClass 的另一个方法中的代码Main() 调用。)static
关键字使得该方法无需实例
即可访问。示例类
。因此 Main 方法是一个入口点,必须声明为静态。为了避免无法解决的
循环依赖
main 被用作入口点引用:http://en.wikipedia.org/wiki/C_Sharp_(programming_language
The
.NET runtime
calls theMain
method. (Note: Main may also be called from elsewhere, e.g. from the code Main() in another method of ExampleClass.) Thestatic
keyword makes the method accessible without aninstance
ofExampleClass
. So Main method is an entry point and must be declared static.To avoid that irresolvable
circular dependency
main is used as an entry pointreference : http://en.wikipedia.org/wiki/C_Sharp_(programming_language
静态方法可以在不创建实例的情况下执行。按照惯例,它们将
main
方法作为默认调用方法。Static methods can be executed without creating an instance. By convention, they have the
main
method as the default method to call.对于包含主方法和其他方法和变量的类的每个对象,所有对象包含的每个变量和方法都有单独的副本,但主类副本只是它们之间的一个,因此要在我们拥有的多个对象之间进行复制使 main 方法成为静态方法。
for every objects of a class contains main method and other methods and variables , there are separate copies of each variable and methods contained by all objects but a main class copy is only one between them and so to make a copy between number of objects we have to make main method as static.