什么是退出一个命令行程序的preferred方式? [英] What's the preferred way of exiting a command line program?

查看:136
本文介绍了什么是退出一个命令行程序的preferred方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这应该是简单的。我只需要简单地退出我的命令行的C#程序 - 没有花哨的东西

This should be straightforward. I just need to simply exit my commandline c# program - no fancy stuff.

我应该使用

Environment.Exit();

this.Close();

或其他什么东西?

or something else?

推荐答案

使用返回; 主要方法。照片 如果您不是在main方法,当你决定要退出程序,你需要从目前执行的主要方法,该方法返回。

Use return; in your Main method.
If you aren't in the main method when you decide to exit the program, you need to return from the method that Main method currently executes.

例如:

void Main(...)
{
    DisplayAvailableCommands();
    ProcessCommands();
}

void ProcessCommands()
{
    while(true)
    {
        var command = ReadCommandFromConsole();
        switch(command)
        {
            case "help":
                DisplayHelp();
                break;
            case "exit":
                return;
        }
    }
}

这是不是一个真正的控制台应用程序的良好的整体设计的一个例子,但它说明了这一点。

This is not really an example of good overall design of a console application, but it illustrates the point.

这篇关于什么是退出一个命令行程序的preferred方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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