使用 Return 来结束应用程序。正确的?
我在 Main 方法的主体中有非常简单的应用程序。我试图找到除了Environment.Exit()之外结束应用程序的不同方法,但我完全忘记了我可以只调用return。如果它只是在 Main 方法中运行,则使用它来结束应用程序是否正确?我看不到任何问题,但我想尽可能多地学习。谢谢
I have very simple application just in the body of Main method. I have tried to find out different ways of ending the app except for Environment.Exit() but I entirely forgot I can just call return. Is it correct to use it for ending the app if it just run in Main method? I cannot see any problem but I want to learn as much as I can. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,听起来你走在正确的轨道上。目前尚不清楚您是否正在运行控制台应用程序,此代码应该有助于突出显示。
Right, sounds like you're on the right track. It's not clear if you're running a console application, and this code should help highlight.
如果您由于错误而退出,那么您确实应该使用带有非零参数的
Environment.Exit
...记录什么类型的错误返回哪个退出代码也是一个好主意。否则,
return
应该没问题……但这确实是一种风格选择。If you're exiting out due to an error, you really should use
Environment.Exit
with a non-zero argument... documenting what type of error returns which exit code is a good idea, too.Otherwise, it should be OK to just
return
... but it's really a stylistic choice.是的,从Main方法返回就可以了。
Yes, it is OK to return from the Main method.