How to clear screen using C#?

CsharpProgrammingServer Side Programming

Use the Console.Clear() method to clear screen and the console buffer. When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window.

Here, we have cleared the screen and then set the ForegroundColor and BackgroundColor −

ConsoleColor newForeColor = ConsoleColor.Blue;
ConsoleColor newBackColor = ConsoleColor.Yellow;

The following is the complete code −

Example

 Live Demo

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      ConsoleColor foreColor = Console.ForegroundColor;
      ConsoleColor backColor = Console.BackgroundColor;
      Console.WriteLine("Clearing the screen!");
      Console.Clear();
      ConsoleColor newForeColor = ConsoleColor.Blue;
      ConsoleColor newBackColor = ConsoleColor.Yellow;
   }
}

Output

Clearing the screen!
raja
Published on 04-Sep-2018 11:02:02
Advertisements