How to Keep Console Window Open in Visual Studio?

How to Keep Console Window Open in Visual Studio?

  • Blog
  • 2 mins read

In the Visual Studio, you are writing a C# (C-Sharp) program to print text on the console window using Console.WriteLine() method. But when you are running the application using the Start button or by pressing F5 (debug mode), console window closes automatically. Here is how you can keep the console window open in Visual Studio in debug mode and in without debug mode.

To Keep Console Window Open in Debug Mode

When you run the application by pressing the Start button or by pressing F5, the Visual Studio executes the application in debug mode. To keep the console window active you should put the Console.ReadLine() command below the Console.WriteLine() command in your C# program as shown in the below example:

class Hello
{
    static void Main(string[] args)
    {
        System.Console.WriteLine("Hello World!");
	System.Console.ReadLine();
    }
}

Output

Hello World!
_

Run Application Without Debugging To Keep Console Window Open

To keep the console window open in Visual Studio without using the Console.ReadLine() method, you should run the application without debug mode by pressing Ctrl+F5 or by clicking on the menu Debug > Start without Debugging option. This way the application remains active below until the user presses a key.

class Hello
{
    static void Main(string[] args)
    {
        System.Console.WriteLine("Hello World!");
    }
}

Output

Hello World!
Press any key to continue . . .

See also: