Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> C#

Cách xóa màn hình console trong C#

Console.Clear() xóa bộ đệm màn hình console và đặt con trỏ về góc trên bên trái (0,0).

Ví dụ

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        ConsoleColor foreColor = Console.ForegroundColor;
        ConsoleColor backColor = Console.BackgroundColor;
        Console.WriteLine("Đang xóa màn hình!");
        Console.Clear();
        ConsoleColor newForeColor = ConsoleColor.Blue;
        ConsoleColor newBackColor = ConsoleColor.Yellow;
    }
}

Kết quả

Đang xóa màn hình!

Lưu ý: Sau Clear, màu nền/màu chữ không tự động khôi phục. Ví dụ trên chỉ demo lưu màu cũ.