Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to clear screen using C#?
The Console.Clear() method in C# is used to clear the console screen and its buffer. When this method is called, the cursor automatically moves to the top-left corner of the console window, effectively giving you a clean slate to work with.
Syntax
Following is the syntax for the Console.Clear() method −
Console.Clear();
How It Works
The Console.Clear() method performs the following actions −
-
Clears all text and content from the console window
-
Resets the cursor position to (0, 0) ? the top-left corner
-
Preserves the current console colors and properties
-
Does not affect the console buffer size or window dimensions
Using Console.Clear() with Color Settings
Example
using System;
using System.Threading;
class Program {
static void Main() {
Console.WriteLine("This text will be cleared in 2 seconds...");
Thread.Sleep(2000);
Console.Clear();
Console.ForegroundColor = ConsoleColor.Blue;
Console.BackgroundColor = ConsoleColor.Yellow;
Console.WriteLine("Screen cleared! New colors applied.");
Console.ResetColor();
}
}
The output of the above code is −
Screen cleared! New colors applied.
Clearing Screen with Menu Display
Example
using System;
class Program {
static void Main() {
for (int i = 1; i
The output of the above code is −
=== CLEARED SCREEN ===
Welcome to the new screen!
Previous content has been removed.
Preserving and Restoring Console Colors
Example
using System;
class Program {
static void Main() {
ConsoleColor originalForeColor = Console.ForegroundColor;
ConsoleColor originalBackColor = Console.BackgroundColor;
Console.WriteLine("Original colors displayed here");
Console.ForegroundColor = ConsoleColor.Green;
Console.BackgroundColor = ConsoleColor.DarkBlue;
Console.WriteLine("Modified colors before clearing");
Console.Clear();
Console.WriteLine("Screen cleared - colors preserved");
Console.ForegroundColor = originalForeColor;
Console.BackgroundColor = originalBackColor;
Console.WriteLine("Colors restored to original");
}
}
The output of the above code is −
Screen cleared - colors preserved
Colors restored to original
Common Use Cases
-
Menu Systems − Clear screen between different menu options
-
Game Development − Refresh the display for console-based games
-
Data Display − Clear old data before showing updated information
-
User Interface − Create a clean interface experience
Conclusion
The Console.Clear() method is a simple yet effective way to clear the console screen in C# applications. It removes all content, resets the cursor position, and maintains current color settings, making it ideal for creating clean, organized console interfaces.
