6

I've done some reading here, and here and I'm still confused about if I should use Enviorment.Exit() in my console application.

In a method, I have the following code if the user types exit at the prompt,

if(userSelect == "exit")
{
    Environment.Exit(0);
}

Update:

class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine("Welcome to my Console App");
        Console.WriteLine();
        consoleManager();
    }


    public static void consoleManager()
    {
        string consolePrompt = "ConsoleApp\">";

        string whichMethod = "";
        Console.Write(consolePrompt);
        whichMethod = Console.ReadLine();

        if(whichMethod == "view enties")
        {
            viewEntry();
        }
        else
            if(whichMethod == "Add Entry")
            {
                addEntry();
            }
            else
                if(whichMethod == "exit")
                {
                    //what to do here
                }
                else
                {
                    help();
                }
    }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    I think the accepted answer from your second link says it all – Rhumborl Feb 14 '16 at 16:09
  • Do note that it is unusual to exit from an application anywhere other than the `main` method. And if you're in the `main` method, a simple `return 0;` statement is sufficient and preferable. – Cody Gray - on strike Feb 14 '16 at 18:12
  • @CodyGray - I updated my code, could you show me how to exit my app properly from my method? –  Feb 14 '16 at 18:42
  • You've now removed the code, I'm not sure why. There are two possibilities: (1) move that code into the `main` method (there's no reason for it to be in its own function), (2) change that function to return a value—either false to close, or an int that corresponds to the return value from main. Check the result of that function inside of your main method and return accordingly. – Cody Gray - on strike Feb 14 '16 at 18:54
  • It is not that `Environment.Exit` doesn't work (although it doesn't work well in a multi-threaded application, but you aren't writing one), it's just that a well-designed application generally doesn't find a need to use it. There is only one case when its use makes sense, and that is in some kind of menu where the user can choose to exit, and that can be written sensibly to just return from main. – Cody Gray - on strike Feb 14 '16 at 18:56
  • @CodyGray - If I move the code to main, I can use Environment.Exit(0); Correct? as I'm indeed creating a menu that the user can choose exit from. –  Feb 14 '16 at 19:03
  • It can also happen as a result of an [assert](https://en.wikipedia.org/wiki/Assertion_(software_development)#Assertions_for_run-time_checking), in which case [the exit code may be 134](https://stackoverflow.com/questions/2862731/when-assert-fails-what-is-the-program-exit-code/2862748#comment125097511_2862759)... – Peter Mortensen Jan 18 '22 at 23:39

1 Answers1

7

Go through the MSDN Documentation for - Environment.Exit, it explains the paramter

the exitCode parameter, use a non-zero number to indicate an error. In your application, you can define your own error codes in an enumeration, and return the appropriate error code based on the scenario. For example, return a value of 1 to indicate that the required file is not present, and a value of 2 to indicate that the file is in the wrong format.

So if you say Exit(0), you will say your process exited successfully without any error. If you use any System Error Code, you are notifying the operating system what when wrong. In your case Environment.Exit(0) is sufficient.

Edit I am pretty sure, you are overthinking it. Points to consider -

  1. If you are exiting from Main a simple return statement would be sufficient.
  2. Elsewhere use Enviorment.Exit(), because return statement will return you to the Main method and program will still continue to run.
  3. I hope you are familiar with void Main(string[] args) and int Main(string[] args). The difference is the return value. You use it incase you want to return a error code, 0 for success and other numbers for various errors. You can very well use the System Error Code linked above.
  4. It boils down to the point - if the caller of your console program uses the return value or not. If its a dos prompt where you are running it will be ignored. If some external application is running your console it may process your output.
TaW
  • 53,122
  • 8
  • 69
  • 111
Carbine
  • 7,849
  • 4
  • 30
  • 54
  • If it's sufficient, then why did Cody Gray say that it's unusual? What's unusual about it? –  Feb 14 '16 at 18:32