为什么 C# 使用隐式 void Main?

发布于 2024-08-23 15:58:01 字数 280 浏览 2 评论 0原文

我不明白为什么 C# 的 Main 函数默认为 void(例如在控制台项目中)。在 C 和 C++ 中,标准明确规定 main 必须返回 int,并且使用返回值是有意义的,因为我们可以检查外部程序的返回值,并查看 C/C++ 应用程序是否成功完成或遇到错误。

所以我的问题是:

  1. 为什么 Visual Studio 将 Main 声明为 void?
  2. C# 控制台应用程序执行完毕后,将值返回到操作系统的最佳方法是什么?

I don't understand why C#'s Main function is void by default (in a console project for example). In C and C++ the standard clearly says main must return int, and using a return value makes sense because we can check that return value from an external program and see if the C/C++ application finished successfully or encountered an error.

So my questions are:

  1. Why does Visual Studio declare Main as void?
  2. What's the best way of returning a value to the OS once a C# console application has finished executing?

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

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

发布评论

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

评论(8

厌味 2024-08-30 15:58:01

您可以使用 intvoid 作为返回类型。因此,只需更改它并返回一个值,就像在 C++ 中一样。

也许它默认是无效的,以免让初学者感到困惑。

You can use either int or void as a return type. Thus, simply change it and return a value like in C++.

Maybe it's void by default in order not to puzzle beginners.

一页 2024-08-30 15:58:01

在 C# 中,您可以使用参见 MSDN

 static int Main() 
 static int Main(string[] args)
 static void Main() 
 static void Main(string[] args)

您还可以通过两种方式返回 (int) 值。

在控制台应用程序中,我将使用 int Main() { ...;返回2; }

在 WinForms/WPF/... 应用程序中,在极少数情况下它需要返回值,我会使用
Environment.ExitCode = 1;Environment.Exit(1);

In C#, you can use, see MSDN :

 static int Main() 
 static int Main(string[] args)
 static void Main() 
 static void Main(string[] args)

You can also return a (int) value in 2 ways.

In a Console application I would use int Main() { ...; return 2; }

In a WinForms/WPF/... app, in the rare situation it needs a return value, I would use
Environment.ExitCode = 1; or Environment.Exit(1);

氛圍 2024-08-30 15:58:01

它并不是隐式无效的。例如,您不能只声明 main(String[] args) 并将其编译为 void 函数。默认项目将 main() 声明为 void,因为默认项目没有从 main 返回任何有用的内容。

还值得注意的是,C# 不是 C 或 C++。有些语法是相同的,但差异要大得多。

It's not implicitly void. As in, you can't just declare main(String[] args) and have it compile as a void function. The default projects declare main() as void because the default projects don't have anything useful to return from main.

It's also worth noting that C# is not C or C++. Some of the syntax is the same, but the differences are far vaster.

揽清风入怀 2024-08-30 15:58:01

我无法说出 C# 开发人员决定将 Main 声明为 void 的原因。至于应用程序完成后将值返回给操作系统,您可以使用以下命令:

System.Environment.ExitCode = 1; // Or whatever number other than 0 for errors.

The reasons behind why C#'s developers decided to have Main declared as void I couldn't say. As far as returning values to the OS when the application has finished you can use this:

System.Environment.ExitCode = 1; // Or whatever number other than 0 for errors.
简单气质女生网名 2024-08-30 15:58:01

您可以更改 Main 的返回类型,而不会引起任何问题。

You can change the return type of Main without causing any problems.

美煞众生 2024-08-30 15:58:01

请记住,您的程序周围还有另一个“盒子”——CLR。当您的代码引发未处理的异常时,它将返回错误代码。

Remember, that there is still another "box" around your program - the CLR. It will return error codes, when your code throws an unhandled exception.

小猫一只 2024-08-30 15:58:01

返回退出值的最佳方法是使用。我这样说是因为非零退出代码通常用于表示某些错误,通常是您不想继续的错误。使用非零退出值调用 Exit 既可以通过该值发出错误信号,也可以退出程序。

Environment.Exit( value );

编辑:请注意,我的语句的上下文位于控制台程序的 Main 内。库和其他方法应该抛出异常或返回错误值。只有您的主例程应该调用 Exit。抱歉,如果不清楚。

Best way to return an exit value is to use. I say this because non-zero exit codes are usually used to signify some error, typically one that you wouldn't want to continue from. Calling Exit with a non-zero exit value both signals the error through the value and exits the program.

Environment.Exit( value );

EDIT: Note, the context for my statement was within Main for a console program. Libraries and other methods should throw exceptions or return error values. Only your main routine should call Exit. Sorry if this wasn't clear.

三生一梦 2024-08-30 15:58:01

您可以使用 Environment.Exit() 终止并向操作系统返回整数代码。或者,您可以设置Environment.ExitCode,只要您的程序正常终止,该代码就会返回到操作系统。

You can use Environment.Exit() to terminate and return an integer code to the OS. Alternatively, you can set Environment.ExitCode and that code will be returned to the OS whenever your program terminates normally.

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