This should be straightforward. I just need to simply exit my commandline c# program - no fancy stuff.
Should I use
Environment.Exit();
or
this.Close();
or something else?
This should be straightforward. I just need to simply exit my commandline c# program - no fancy stuff.
Should I use
Environment.Exit();
or
this.Close();
or something else?
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.
Example:
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.
just return from the Main
method.
Edit:
if you really have lost the flow and want to exit from anywhere in the application (like inside any method called by Main), you can use:
Environment.Exit(0);
remember that normally you should return 0 to the calling process (OS) when everything went fine and you return a non zero value if an error happened and execution did not go as smooth as should have been.