[2.0] How to do a clean exit from console application.
Based on the result of my checking, I would like to exit out from the console application decently without throwing any exceptions.
static void Main(string[] args)
{
//Check to see if today is a US Holiday.
If(Holiday.IsUSHoliday(DateTime.Today));
//close the program and how to do that?
}
Re: [2.0] How to do a clean exit from console application.
Re: [2.0] How to do a clean exit from console application.
Quote:
Originally Posted by mendhak
Try:
return;
it works in C, C++, Java and I bit it'd work in C# ;)
Re: [2.0] How to do a clean exit from console application.
Using the return keyword anywhere except for the last line of a procedure is spaghetti code. You should structure your if{} blocks in a manner such that if the condition arises where the application should exit, the code path naturally jumps to the end of the Main() procedure.
For example:
Code:
// Bad:
static void Main(string[] args)
{
if (Holiday.IsUSHoliday(DateTime.Today)) {
return;
}
// rest of code
}
// Good:
static void Main(string[] args)
{
if (!Holiday.IsUSHoliday(DateTime.Today)) {
// rest of code
}
}
Re: [2.0] How to do a clean exit from console application.
Quote:
Originally Posted by penagate
Using the return keyword anywhere except for the last line of a procedure is spaghetti code.
Does this mean that Italians aren't very good coders?
Re: [2.0] How to do a clean exit from console application.
you could just use
Code:
Environment.Exit(0);
when you want to exit from the program. more info on msdn.