This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Clearing the Console

When developing console applications, it is common to need to clear the screen before requesting new input or displaying new content. This is simple to achieve with a single method call.

Console Class

The Console class provides functionality that is useful when you are developing console applications. It includes methods that allow you to display strings in a textual user interface and to receive input, from identifying single key presses to requesting large amounts of text.

It is common in a console application to display text in lines, starting at the current cursor position and moving downwards. When the bottom of the screen is reached, the console scrolls to make space for more information. For some applications this is not ideal and it would be better to clear the screen before displaying more text or requesting further input.

To clear the screen using the Console class, you can simply call the static method, Clear. This removes all of the currently visible text and all of the text buffer that has scrolled off the top of the screen. This prevents the user from scrolling the console window to see the previously hidden text. The effect of the method is the same as executing the cls command from the console.

After clearing the text, the cursor is moved to the top left corner, so further output begins at the top of the screen. If the background colour of the console has been changed, using the BackgroundColor property, the entire console background will be set to the new colour.

18 January 2014