How to terminate a .NET console application with an exit code
October 2, 2015 1 Comment
It happens that you’d like to terminate a .NET console application with some exit code. Normally exit codes are integers where negative numbers indicate that something has gone wrong with -1 being the most common. Positive digits and zero on the other hand usually imply a program execution with no errors. The most common positive exit codes are 0 and 1.
The Environment object has a method called Exit that accepts an integer as the exit code:
static void Main(string[] args) { Environment.Exit(-1); }
That’s all you need if you’d like to terminate a .NET console application with some exit code.
View all various C# language feature related posts here.
Thanks so much for sharing this tip. I am kind-of new to C# and was trying to figure out how I could cleanly exit an app if one of my methods/classes failed. Like usual, I was overthinking this.
(Sometimes, the easiest way to meet someone is to simply say hello!)