此方法用于清除控制台缓冲区和相应的显示信息控制台窗口。
Syntax: public static void Clear ();
Exceptions: This method throws IOException if an I/O error occurred.
以下程序显示Console.Clear()方法的用法:
程序1:在使用Clear方法之前显示内容
// C# program to illustrate the use
// of Console.Clear Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GFG {
class Program {
static void Main(string[] args)
{
// Print the statements
Console.WriteLine("GeeksForGeeks");
Console.WriteLine("A Computer Science Portal");
Console.WriteLine("For Geeks");
}
}
}
输出:
GeeksForGeeks
A Computer Science Portal
For Geeks
程序2:使用clear()方法清除控制台
// C# program to illustrate the use
// of Console.Clear Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace GFG {
class Program {
static void Main(string[] args)
{
// Print the statements
Console.WriteLine("GeeksForGeeks");
Console.WriteLine("A Computer Science Portal");
Console.WriteLine("For Geeks");
// Clear the Console
Console.Clear();
}
}
}
输出: