Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Console.Clear Method

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

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

public static void Clear ()
public static void Clear ()
public static function Clear ()
Not applicable.

Exception typeCondition

IOException

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.

NoteNote:

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.

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)
    {
    try
        {
        Console.SetCursorPosition(origCol+x, origRow+y);
        Console.Write(s);
        }
    catch (ArgumentOutOfRangeException e)
        {
        Console.Clear();
        Console.WriteLine(e.Message);
        }
    }

    public static void Main() 
    {
// Clear the screen, then save the top and left coordinates.
    Console.Clear();
    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);
    Console.WriteLine();
    }
}
/*
This example produces the following results:

+---+
|   |
|   |
|   |
+---+

All done!

*/

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

import System.*;

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

    protected static void WriteAt(String s, int x, int y) 
    {
        try {
            Console.SetCursorPosition(origCol + x, origRow + y);
            Console.Write(s);
        }
        catch (ArgumentOutOfRangeException e) {
            Console.Clear();
            Console.WriteLine(e.get_Message());
        }
    } //WriteAt

    public static void main(String[] args)
    {
        // Clear the screen, then save the top and left coordinates.
        Console.Clear();
        origRow = Console.get_CursorTop();
        origCol = Console.get_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);
        Console.WriteLine();
    } //main
} //Sample
/*
This example produces the following results:

+---+
|   |
|   |
|   |
+---+

All done!

*/

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft