Console.Clear Method ()


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Clears the console buffer and corresponding console window of display information.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static void Clear()

Exception Condition

An I/O error occurred.

Using the Clear method is equivalent invoking the MS-DOS cls command in the command prompt window. When the Clear method is called, the cursor automatically scrolls to the top-left corner of the window and the contents of the screen buffer are set to blanks using the current foreground background colors.


Attempting to call the Clear method when a console application's output is redirected to a file throws a IOException. To prevent this, always wrap a call to the Clear method in a trycatch block.

The following example uses the Clear method to clear the console before it executes a loop, prompts the user to select a foreground and background color and to enter a string to display. If the user chooses not to exit the program, the console's original foreground and background colors are restored and the Clear method is called again before re-executing the loop.

using System;

public class Example
   public static void Main()
      // Save colors so they can be restored when use finishes input.
      ConsoleColor dftForeColor = Console.ForegroundColor;
      ConsoleColor dftBackColor = Console.BackgroundColor;
      bool continueFlag = true;

      do { 
         ConsoleColor newForeColor = ConsoleColor.White;
         ConsoleColor newBackColor = ConsoleColor.Black;

         Char foreColorSelection = GetKeyPress("Select Text Color (B for Blue, R for Red, Y for Yellow): ", 
                                              new Char[] { 'B', 'R', 'Y' } );
         switch (foreColorSelection) {
            case 'B':
            case 'b':
               newForeColor = ConsoleColor.DarkBlue;
            case 'R':
            case 'r':
               newForeColor = ConsoleColor.DarkRed;
            case 'Y':
            case 'y':
               newForeColor = ConsoleColor.DarkYellow;
         Char backColorSelection = GetKeyPress("Select Background Color (W for White, G for Green, M for Magenta): ",
                                              new Char[] { 'W', 'G', 'M' });
         switch (backColorSelection) {
            case 'W':
            case 'w':
               newBackColor = ConsoleColor.White;
            case 'G':
            case 'g':
               newBackColor = ConsoleColor.Green;
            case 'M':
            case 'm':
               newBackColor = ConsoleColor.Magenta;

         Console.Write("Enter a message to display: ");
         String textToDisplay = Console.ReadLine();
         Console.ForegroundColor = newForeColor;
         Console.BackgroundColor = newBackColor;
         if (Char.ToUpper(GetKeyPress("Display another message (Y/N): ", new Char[] { 'Y', 'N' } )) == 'N')
            continueFlag = false;

         // Restore the default settings and clear the screen.
         Console.ForegroundColor = dftForeColor;
         Console.BackgroundColor = dftBackColor;
      } while (continueFlag);

   private static Char GetKeyPress(String msg, Char[] validChars) 
      ConsoleKeyInfo keyPressed;
      bool valid = false;

      do {
         keyPressed = Console.ReadKey();
         if (Array.Exists(validChars, ch => ch.Equals(Char.ToUpper(keyPressed.KeyChar))))           
            valid = true;

      } while (! valid);
      return keyPressed.KeyChar; 

The example relies on a GetKeyPress method to validate the user's selection of a foreground and background color.

This example demonstrates the CursorLeft and CursorTop properties, and the SetCursorPosition and Clear methods. The example positions the cursor, which determines where the next write will occur, to draw a 5 character by 5 character rectangle using a combination of "+", "|", and "-" strings. Note that the rectangle could be drawn with fewer steps using a combination of other strings.

// This example demonstrates the 
//     Console.CursorLeft and 
//     Console.CursorTop properties, and the
//     Console.SetCursorPosition and 
//     Console.Clear methods.
using System;

class Sample 
    protected static int origRow;
    protected static int origCol;

    protected static void WriteAt(string s, int x, int y)
        Console.SetCursorPosition(origCol+x, origRow+y);
    catch (ArgumentOutOfRangeException e)

    public static void Main() 
// Clear the screen, then save the top and left coordinates.
    origRow = Console.CursorTop;
    origCol = Console.CursorLeft;

// Draw the left side of a 5x5 rectangle, from top to bottom.
    WriteAt("+", 0, 0);
    WriteAt("|", 0, 1);
    WriteAt("|", 0, 2);
    WriteAt("|", 0, 3);
    WriteAt("+", 0, 4);

// Draw the bottom side, from left to right.
    WriteAt("-", 1, 4); // shortcut: WriteAt("---", 1, 4)
    WriteAt("-", 2, 4); // ...
    WriteAt("-", 3, 4); // ...
    WriteAt("+", 4, 4);

// Draw the right side, from bottom to top.
    WriteAt("|", 4, 3);
    WriteAt("|", 4, 2);
    WriteAt("|", 4, 1);
    WriteAt("+", 4, 0);

// Draw the top side, from right to left.
    WriteAt("-", 3, 0); // shortcut: WriteAt("---", 1, 0)
    WriteAt("-", 2, 0); // ...
    WriteAt("-", 1, 0); // ...
    WriteAt("All done!", 0, 6);
This example produces the following results:

|   |
|   |
|   |

All done!


.NET Framework
Available since 2.0
Return to top