Hi, 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
+1
A:
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.
static void Main(string[] args)
{
bool keepRunning = true;
int x = 0;
while (keepRunning)
{
//at some point, flip keepRunning to fall out of the while loop
if (moon.Color == Blue)
keepRunning = false;
if (x > 0) //or some other condition
return;
}
//naturally will fall out of the Main() and terminate the program
}
p.campbell
2010-09-24 14:58:58
+1
A:
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.
R. Bemrose
2010-09-24 14:59:02
You can also declare `Main` to return `int`, and return your exit code. You don't have to call `Environment.Exit`.
P Daddy
2010-09-24 15:04:00
@PDaddy: I wasn't aware that C# let you do that. Whoops.
R. Bemrose
2010-09-24 15:23:33