C#:使用Environment.ExitCode与从主返回int [英] C#: Using Environment.ExitCode versus returning int from Main

查看:1310
本文介绍了C#:使用Environment.ExitCode与从主返回int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算用C#可执行文件的返回码在我的shell脚本之一。我有两个选择:

I am planning to use the return code of the C# executable in one of my shell script. I have two options:

从主方法返回一个int值

class MainReturnValTest
{
    static int Main()
    {
        //...
        return 0;
    }
}



(OR)

使用Environment.Exit与退出代码

class MainReturnValTest
{
    static void Main()
    {
        //...
        Enviroment.Exit(exitCode);
    }
}



它是精细使用上述任何方式从可执行的返回值?或者是优于其它其中之一吗?

Is it fine to use any of the above ways to return value from the executable? Or is one of them preferred over other?

推荐答案

Environment.Exit()是一个粗鲁的中止。它立即终止该处理。只有当你发现一个严重不合格使用它,它是例如在发生AppDomain.UnhandledException事件处理适当。哪当你的程序是关于因为未处理的异常终止运行。

Environment.Exit() is a rude abort. It instantly terminates the process. Use it only when you detect a gross failure, it is appropriate in an AppDomain.UnhandledException event handler for example. Which runs when your program is about to terminate because of an unhandled exception.

你是哪个领导:异常是信号应与终止程序异常情况的好方法一个的ExitCode不是零。实际上,它会自动被设置为异常的HResult的属性值。无需任何代码。

Which is your lead: exceptions are a good way to signal unusual conditions that should terminate the program with an ExitCode that isn't zero. In fact, it automatically gets set to the HResult property value of the exception. No code required.

这篇关于C#:使用Environment.ExitCode与从主返回int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆