views:

1674

answers:

3

I'm maintaining a number of console applications at work and one thing I've been noticing in a number of them is that they call Environment.Exit(0).

A sample program would look like this:

public class Program
{
    public static void Main(string[] args)
    {
        DoStuff();
        Environment.Exit(0);
    }
}

I don't understand what the intent of the original programmer was in doing this? In my mind even without the Environment.Exit statement the program should exit just fine. That said, for one of these programs, it's console window has been remaining even after it was supposed to have closed so I'm really not sure what's going on there....

Am I missing something here? Or is there a reason why Environment.Exit should be called in this instance?

+1  A: 

Basically the statement Environment.Exit(0) tells the Operating system that this is a "clean" exit. There are other numbers as well each with a different meaning like Environment.Exit(1)

However one thing to note is that the "Main" has been declared as returning nothing "void", so the exit code will really not have a meaning to it.

Just in case you wanted to know more about the different exit codes have a look here

system Error Codes

Anand
+2  A: 

This is compatibility for command-line programs that indicate their success. This comes from older C-style main loops where the prototype of the main function was

int main(void);

int main(int argc, char *argv[]);

The return value of 0 traditionally meant success, while non-zero meant failure or something else, depending on what the programmer decided.

Reference:

wiki for more information on the main function.

MSDN documenttion on Environment.Exit()

Environment.Exit() : Terminates this process and gives the underlying operating system the specified exit code.

Robert Paulson
In my case, my console application is a stand alone application and doesn't need to tell the OS anything. So I guess that means the call is redundant in my situation....?
mezoid
It's more about telling the shell program if your program succeeded or failed, which may be important down the road, so go with the convention. e.g. batch files, nAnt integration, installers, etc.
Robert Paulson
+4  A: 

The only reason for calling Exit() as the last line of the Main method is if there might be other foreground threads running. They would stay running if execution just fell off the end of Main. Even in this case, it would usually be a better idea either to put in some explicit graceful termination into the other threads - or make them background threads to start with.

If you ever want to return a different exit code from Main, the simpler way to achieve that is to declare it to return int.

In short, I don't think you need Environment.Exit() here, and it's worth asking your colleagues exactly why they're using it - chances are they won't be able to give you a good reason, and it's another bit of fluff you can cut out.

Jon Skeet
Thanks jon! Don't think I'll have much luck asking the original developer about it since she is on maternaty leave. At least I now know that I can remove it without worrying about breaking something.
mezoid