Why does the compiler think Environment.Exit can return?

混江龙づ霸主 提交于 2019-12-14 03:31:54

问题


Example code:

switch(something)
{
    case 0:
        System.Environment.Exit(0);
    case 1:
        // blah ...
        break;
}

It won't compile because the compiler thinks that execution can return from Exit(). The compiler is obviously wrong.

No tricks. System.Environment.Exit() is the real one.

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.


回答1:


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.

You'll need to add the "break" in there to make the compiler happy.



来源:https://stackoverflow.com/questions/45992172/why-does-the-compiler-think-environment-exit-can-return

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!