How to exit from a console application

.NET programming topics
Post Reply
Cyclops
Lieutenant
Lieutenant
Posts: 71
Joined: Wed Jul 15, 2009 1:48 pm
Location: London

How to exit from a console application

Post by Cyclops » Thu Dec 31, 2009 9:08 am

Use Environment.Exit to immediately return from a console application with a specified result code.

Code: Select all

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");
    }
  }
}
Post Reply

Return to “.NET Programming”