为什么大多数 C# 程序中的 main 方法入口点是静态的?

发布于 2024-08-23 17:00:47 字数 37 浏览 9 评论 0原文

为什么大多数 C# 程序中的 main 方法入口点是静态的?

Why is the main method entry point in most C# programs static?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

肩上的翅膀 2024-08-30 17:00:47

为了调用实例方法,您需要一个对象的实例。这意味着为了启动您的程序,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 method Main. Hence the constructor of Program would run before Main which defeats the purpose of having a main altogether.

沦落红尘 2024-08-30 17:00:47

我想把问题转过来。实现允许 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?

温馨耳语 2024-08-30 17:00:47

从概念上讲,您只有一个静态实例。静态方法很好地映射到程序的单个起始点的习惯用法。语言设计者本可以创建一个特殊的程序类来与 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.

从﹋此江山别 2024-08-30 17:00:47

因为否则它必须创建一个对象,并且运行构造函数可能会导致负面影响。

Because otherwise it would have to create an object, and running the constructor could cause negative side effects.

懒的傷心 2024-08-30 17:00:47

否则你怎么能在 main 之前创建你的类实例呢?

How could you create your class instance before main otherwise?

甜中书 2024-08-30 17:00:47

.NET 运行时 调用Main 方法。 (注意:Main 也可以从其他地方调用,例如从ExampleClass 的另一个方法中的代码Main() 调用。)static 关键字使得该方法无需实例 即可访问。 示例类。因此 Main 方法是一个入口点,必须声明为静态。

否则,程序将需要
一个实例,但任何实例都会
需要一个程序。

为了避免无法解决的循环依赖 main 被用作入口点


引用:http://en.wikipedia.org/wiki/C_Sharp_(programming_language

The .NET runtime calls the Main method. (Note: Main may also be called from elsewhere, e.g. from the code Main() in another method of ExampleClass.) The static keyword makes the method accessible without an instance of ExampleClass. So Main method is an entry point and must be declared static.

Otherwise, the program would require
an instance, but any instance would
require a program.

To avoid that irresolvable circular dependency main is used as an entry point


reference : http://en.wikipedia.org/wiki/C_Sharp_(programming_language

梦言归人 2024-08-30 17:00:47

静态方法可以在不创建实例的情况下执行。按照惯例,它们将 main 方法作为默认调用方法。

Static methods can be executed without creating an instance. By convention, they have the main method as the default method to call.

浮生面具三千个 2024-08-30 17:00:47

对于包含主方法和其他方法和变量的类的每个对象,所有对象包含的每个变量和方法都有单独的副本,但主类副本只是它们之间的一个,因此要在我们拥有的多个对象之间进行复制使 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文