为什么c#中的main方法总是放在类内部,而c++中则不然
为什么在 C# 中我们总是将 main()
方法放在 class
内部,而在 C++ 中它总是放在 class
外部。
Why we put main()
method always inside the class
in C# while in c++ it always placed outside of the class
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
C++ 语言设计者效仿了 C 语言,因此 main 函数是一个普通函数。
C# 语言设计者在设计语言时选择所有方法都必须是类的一部分。
The C++ language designers followed the lead of C and so the main function is a plain function.
The C# language designers made the choice, when designing the language, that all methods must be part of classes.
由于历史原因。 C++ 从 C 发展而来,C 具有全局
main()
函数。 C# 更年轻,并且是从头开始设计的。 C# 的设计特点之一是没有全局函数,因此main
函数必须属于一个类。For historical reasons. C++ evolved from C, which had a global
main()
function. C# is much younger and was designed from scratch. One of the design features of C# is the absence of global functions, so themain
function has to belong to a class.因为在 .NET 中,您只能将方法放置在类型内部。你不能让它们在空旷的空间里漂浮。 C++ 继承了 C 的传统,C 不是 OOP 语言,因此您可以在任何地方定义函数。
Because in .NET you can place methods only inside types. You cannot have them floating around in the empty space. C++ has its legacy from C which is not an OOP language so you could define functions anywhere.
在 C# 中,不能将方法放置在类/结构之外。每个方法必须位于类/结构中
You cannot place method outside class/struct in C#. Each method must be in class/struct
这是一个约定。这符合 Java(也遵循类内部有方法的语义)。
It is a convention. Which is in line with Java (also follows the semantic of having a method inside class).
C# 是完全面向对象的语言,一切都被视为对象。因此,Main() 保留在类中。
C# is complete object oriented language where everything is considered as objects. Hence, Main() is kept inside class.