How to Pause Console in C#
-
Pause Console of Visual Studio With the
Console.ReadLine()
Function inC#
-
Pause Console of Visual Studio With the
Console.ReadKey()
Function inC#
-
Pause Console of Visual Studio With the Ctrl+F5 Shortcut in
C#
This tutorial will introduce methods to pause the console of Microsoft Visual Studio IDE in C#.
Pause Console of Visual Studio With the Console.ReadLine()
Function in C#
The Console.ReadLine()
function gets the input from the console in C#. The return type of the Console.ReadLine()
function is string. The Console.ReadLine()
function reads a complete line from the console.
The Console.ReadLine()
function can be used to pause our console after displaying some output. We can pause the console with the Console.ReadLine()
function by writing it at the end of our code.
The following code example shows us how we can pause the console of the Microsoft Visual Studio IDE with the Console.ReadLine()
function in C#.
using System;
namespace pause_console {
class Program {
static void Main(string[] args) {
Console.WriteLine("This is a piece of code");
Console.ReadLine();
}
}
}
Output:
This is a piece of code
We paused our console after displaying This is a piece of code
on the screen with the Console.ReadLine()
function in C#. The console can only be resumed by using the Enter key only. Because the Console.ReadLine()
function takes a complete line from the console. We cannot resume the console by pressing any key.
Pause Console of Visual Studio With the Console.ReadKey()
Function in C#
The Console.ReadKey()
function can also be used to get input from the console in C#. The return type of the Console.ReadLine()
function is an object of the ConsoleKeyInfo
class. The Console.ReadKey()
function reads only a single key from the console. The Console.ReadKey()
function can be used to pause our console after displaying some output. We can pause the console with the Console.ReadKey()
function by writing it at the end of our code.
The following code example shows us how we can pause the console of the Microsoft Visual Studio IDE with the Console.ReadKey()
function in C#.
using System;
namespace pause_console {
class Program {
static void Main(string[] args) {
Console.WriteLine("This is a piece of code");
Console.ReadKey();
}
}
}
Output:
This is a piece of code
We paused our console after displaying This is a piece of code
on the screen with the Console.ReadKey()
function in C#. The console can be resumed by pressing any key. Because the Console.ReadKey()
function reads only one key from the console.
Pause Console of Visual Studio With the Ctrl+F5 Shortcut in C#
The disadvantage of using both of the methods discussed above is that we have to move the functions mentioned above to the end of our code every time we extend our code. The best and the most appropriate way of pausing your console after the execution of code is to run your code with the Ctrl+F5 shortcut. In Visual Studio IDE, when we run our code by clicking the start button, our program runs in the debug mode. If we want to run our code just like it does on languages like C and C++, we have to compile our code with the Ctrl+F5 shortcut. The following code example shows us how we can pause our console with the Ctrl+F5 shortcut in C#.
using System;
namespace pause_console {
class Program {
static void Main(string[] args) {
Console.WriteLine("This is a piece of code");
Console.ReadKey();
}
}
}
Output:
This is a piece of code
This time we run our code with the Ctrl+F5 shortcut and did not need any function to pause the console.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn