C#: Preventing the console from closing at the end of the program
NicolasBrondinBernard
A solution to a problem every developer has already encountered with software running in the console!

Article published on 06/11/2022, last updated on 10/08/2026
You've just created your first program in C#, you run it and... surprise. The console opens and closes without you having had time to see anything?
Don't worry, that's normal! Besides, this isn't a problem encountered only in C#, but in the majority of other languages as well.
The goal of a program is to run correctly, and to use as few resources as possible. So once everything is finished (with or without errors), it closes, regardless of the information it may have written to the console during its execution!
Solution
The solution is simple: you need to make the program wait for you to give it the order to stop! To do this, we'll use the following method:
Console.ReadLine();
The Console.ReadLine method is used to retrieve information entered by the user via the keyboard.
But here, we're not going to store this information: as soon as the user presses the "Enter" key, the program will stop and the console will disappear!
Here's a small example program:
static void Main(string[] args)
{
Console.WriteLine("Je peux lire ceci");
Console.WriteLine("et ceci.");
// Le programme va attendre
// l'appui d'une touche pour se fermer
Console.ReadLine();
}
No spam. Only free content, news, and ever more resources to level up your skills!
Join +1500 developers
No comments yet