CKTIS / C#
Environment.Exit — terminating a process
Environment.Exit(int exitCode) terminates the entire process and passes an exit code to the operating system. It is not an ordinary method return: it also stops other threads and skips finally blocks.
Version: .NET 10; repository net10.0 and LangVersion=preview; feature versions require per-entry verification
Syntax
public static void Exit(int exitCode);
Environment.Exit(exitCode);
System.Environment.Exit(exitCode);Environment.Exit(int exitCode) is a static method available in .NET 10. System.Environment.Exit is its fully qualified name; with using System;, you can use Environment.Exit. Code 0 indicates success, while a nonzero code indicates an error according to the application's convention.
The call terminates the entire process, even when other threads are running. In contrast, return leaves the current method; returning from Main does not terminate the process while foreground threads remain active.
This is not graceful shutdown: finally blocks do not execute. Do not rely on them to save data or release resources after this call. For ordinary control flow, prefer returning from Main and performing orderly cleanup beforehand.
The example prints only Before exit and terminates with code 2; Cleanup does not appear. Run it as a separate process because it also terminates the process hosting the code.
Examples
using System;
internal static class Program
{
private static void Main()
{
try
{
Console.WriteLine("Before exit");
Environment.Exit(2);
}
finally
{
Console.WriteLine("Cleanup");
}
}
}Result
Before exit
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.