| Environment.Exit in C# is a static method that abruptly terminates the current process, exiting the application immediately.
It takes an integer argument representing the exit code, which can be used to signal success (0) or failure (non-zero) to the operating system or other programs. While Environment.Exit provides a way to terminate the program, it's generally considered good practice to use the more controlled approach of returning from the Main method or using the Application.Exit method in Windows Forms or WPF applications. Use Environment.Exit to immediately return from a console application with a specified result code. using System;
namespace ConsoleApplication1
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("About to exit");
int exitCode = 0;
Environment.Exit(exitCode);
// This line is not executed.
Console.WriteLine("After exit");
}
}
}
|