Environment.Exit 和 Main 中的简单返回 2 之间的区别
之间有什么区别吗
...
Environment.Exit(2)
从应用程序的外部来看,和
static int Main()
{
...
return 2;
}
?
From outside of the application, is there any difference between
...
Environment.Exit(2)
and
static int Main()
{
...
return 2;
}
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最明显的区别是您可以从代码中的任何位置调用Environment.Exit。除此之外:
Environment.Exit
无论如何都会终止该进程。Main
返回时,就托管代码而言,您已经处于顶层。Environment.Exit
需要适当的安全权限,因此不适用于不太受信任的应用程序。看到问题更新后,我不完全确定你的意思。在这两种情况下,进程都会退出,代码为 2...
The most obvious difference is that you can call Environment.Exit from anywhere in your code. Aside from that:
Environment.Exit
will take down the process anyway.Environment.Exit
terminates the process without unwinding the stack and executing finally blocks (at least according to my experiments). Obviously when you return fromMain
you're already at the top level as far as managed code is concerned.Environment.Exit
demands the appropriate security permission, so won't work for less trusted apps.Having seen the question update, I'm not entirely sure what you mean. In both cases the process will just exit with a code of 2...
Environment.Exit(2)
可以在任何地方使用。return 2
仅在Main()
函数内。Environment.Exit(2)
can be used everywhere.return 2
only within theMain()
function.如果您正在进行单元测试并调用 Main
,则 Environment.exit 将始终反映失败。使用 return 将按预期工作。
If you are doing a Unit Test and calling Main
then Environment.exit will always reflect a failure. Where as using return will work as expect.