为什么c#中的main方法总是放在类内部,而c++中则不然

发布于 2024-12-28 16:02:09 字数 105 浏览 2 评论 0原文

为什么在 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 技术交流群。

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

发布评论

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

评论(6

温柔戏命师 2025-01-04 16:02:09

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.

澜川若宁 2025-01-04 16:02:09

由于历史原因。 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 the main function has to belong to a class.

泪之魂 2025-01-04 16:02:09

因为在 .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.

淡莣 2025-01-04 16:02:09

在 C# 中,不能将方法放置在类/结构之外。每个方法必须位于类/结构中

You cannot place method outside class/struct in C#. Each method must be in class/struct

静谧 2025-01-04 16:02:09

这是一个约定。这符合 Java(也遵循类内部有方法的语义)。

It is a convention. Which is in line with Java (also follows the semantic of having a method inside class).

芸娘子的小脾气 2025-01-04 16:02:09

C# 是完全面向对象的语言,一切都被视为对象。因此,Main() 保留在类中。

C# is complete object oriented language where everything is considered as objects. Hence, Main() is kept inside class.

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