为什么编译器认为Environment.Exit可以返回? [英] Why does the compiler think Environment.Exit can return?
问题描述
示例代码:
switch(something)
{
case 0:
System.Environment.Exit(0);
case 1:
// blah ...
break;
}
它不会编译,因为编译器认为执行可以从Exit返回( )。编译器显然是错误的。
It won't compile because the compiler thinks that execution can return from Exit(). The compiler is obviously wrong.
没有技巧。 System.Environment.Exit()
是真实的。
No tricks. System.Environment.Exit()
is the real one.
对于<$ c来说,这不仅是完全不合逻辑的$ c> System.Environment.Exit()返回,我跟踪了代码,最终调用了 ExitProcess(exitCode);
Not only is it utterly illogical for System.Environment.Exit()
to return, I traced the code and it eventually calls ExitProcess(exitCode);
which can't return.
推荐答案
就语言而言,它可以返回。是的,在现实生活中,该过程将在有机会返回之前终止,但是编译器基于方法签名不知道该过程。
As far as the language is concerned, it can return. Yes, in real-life the process will terminate before it has a chance to return, but the compiler doesn't know that based on the method signature.
您将需要在其中添加 break以使编译器满意。
You'll need to add the "break" in there to make the compiler happy.
这篇关于为什么编译器认为Environment.Exit可以返回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!