What is the command in C# for exiting a console application?
-
2Does this answer your question? [How do I specify the exit code of a console application in .NET?](https://stackoverflow.com/questions/155610/how-do-i-specify-the-exit-code-of-a-console-application-in-net) – Michael Freidgeim Nov 15 '21 at 06:23
-
The canonical is *[How do I specify the exit code of a console application in .NET?](https://stackoverflow.com/questions/155610/)*. – Peter Mortensen Jan 17 '22 at 22:42
-
For exiting by *[Trace.Assert()](https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace.assert?view=net-6.0)* (usually conditional) and its exit code (134 on some platforms), see [this](https://stackoverflow.com/questions/2862731/when-assert-fails-what-is-the-program-exit-code/2862759#comment125097511_2862759). – Peter Mortensen Jan 18 '22 at 23:21
4 Answers
You can use Environment.Exit(0);
and Application.Exit
Environment.Exit(0)
is cleaner.

- 16,104
- 25
- 61
- 88

- 47,018
- 22
- 121
- 208
-
13
-
This worked perfectly for me when I added a timer to a console app via https://stackoverflow.com/a/7865126/1530166. Application.Exit didn't do it, even when using https://stackoverflow.com/a/37029044/1530166. – Jason Anderson Oct 12 '17 at 16:18
-
6@GrantH. it is cleaner because it gives the underlying operating system a more useful error code why it is exiting. – Fayssal El Mofatiche Nov 08 '17 at 08:23
-
However, `Environment.Exit` requires that you have SecurityPermissionFlag.UnmanagedCode permissions - which might be troublesome for some. – wp78de Sep 11 '18 at 20:47
Several options, by order of most appropriate way:
- Return an int from the Program.Main method
- Throw an exception and don't handle it anywhere (use for unexpected error situations)
- To force termination elsewhere,
System.Environment.Exit
(not portable! see below)
Edited 9/2013 to improve readability
Returning with a specific exit code: As Servy points out in the comments, you can declare Main with an int
return type and return an error code that way. So there really is no need to use Environment.Exit unless you need to terminate with an exit code and can't possibly do it in the Main method. Most probably you can avoid that by throwing an exception, and returning an error code in Main if any unhandled exception propagates there. If the application is multi-threaded you'll probably need even more boilerplate to properly terminate with an exit code so you may be better off just calling Environment.Exit.
Another point against using Evironment.Exit
- even when writing multi-threaded applications - is reusability. If you ever want to reuse your code in an environment that makes Environment.Exit
irrelevant (such as a library that may be used in a web server), the code will not be portable. The best solution still is, in my opinion, to always use exceptions and/or return values that represent that the method reached some error/finish state. That way, you can always use the same code in any .NET environment, and in any type of application. If you are writing specifically an app that needs to return an exit code or to terminate in a way similar to what Environment.Exit
does, you can then go ahead and wrap the thread at the highest level and handle the errors/exceptions as needed.

- 16,205
- 3
- 49
- 80
-
13That last option is in bad taste. No programmer want to exit a program by throwing exception. – Nikhil Agrawal Apr 23 '12 at 18:28
-
5As I wrote "by order of most appropriate". Besides, in certain situations throwing an exception IS the correct way (such as when an unexpected, fatal error that shouldn't happen occurs from which the application will never recover). I've updated the answer to clarify. – sinelaw Apr 23 '12 at 18:33
-
If you're in Main you can just return 0, 1, etc. and mark the method signature as returning an int to return an error code, you don't NEED to use Environment.Exit to specify an error code. – Servy Apr 23 '12 at 18:46
-
2Depends on how deep in the call stack you are and whether you're in the main thread. It can be simpler to call Environment.Exit than to architect your entire program to return all the way back to the main function without using an exception to unwind the call stack (which wouldn't work from a background thread; either the ThreadPool would swallow the exception or would throw it out unhandled, either way any catch logic in the main method or thread would never see it. – KeithS Apr 23 '12 at 18:49
-
I changed the order of the options. Throwing an exception has the advantage of making your code much easier to reuse as a library in different executable environments. – sinelaw Jun 26 '13 at 18:45
Console applications will exit when the main function has finished running. A "return" will achieve this.
static void Main(string[] args)
{
while (true)
{
Console.WriteLine("I'm running!");
return; //This will exit the console application's running thread
}
}
If you're returning an error code you can do it this way, which is accessible from functions outside of the initial thread:
System.Environment.Exit(-1);

- 2,021
- 12
- 11
-
-
A negative value indicates an error to the calling process. This signals to the calling application that it was an error and not a successful run. Returning 0 or a positive integer indicates success. https://www.tutorialspoint.com/batch_script/batch_script_return_code.htm – Developer Jul 13 '22 at 18:34
You can use Environment.Exit(0)
and Application.Exit
.
Environment.Exit()
: terminates this process and gives the underlying operating system the specified exit code.

- 17,306
- 24
- 81
- 109

- 926
- 10
- 34