C# 中的 Console.Clear 方法
csharpserver side programmingprogramming更新于 2025/10/9 1:57:00
C# 中的 Console.Clear 方法用于清除控制台缓冲区和相应的控制台窗口的显示信息。
语法
以下是语法 −
public static void Clear ();
示例
在实现 Console.Clear 方法之前,让我们先看一个例子 −
using System;
public class Demo {
public static void Main(){
Uri newURI1 = new Uri("https://www.tutorialspoint.com/");
Console.WriteLine("URI = "+newURI1);
Console.WriteLine("String representation = "+newURI1.ToString());
Uri newURI2 = new Uri("https://www.tutorialspoint.com/jquery.htm#abcd");
Console.WriteLine("
URI = "+newURI2);
Console.WriteLine("String representation = "+newURI2.ToString());
if(newURI1.Equals(newURI2))
Console.WriteLine("
Both the URIs are equal!");
else
Console.WriteLine("
Both the URIs aren't equal!");
Uri res = newURI1.MakeRelativeUri(newURI2);
Console.WriteLine("Relative uri = "+res);
}
}
输出
这将在使用 Console.Clear() 之前产生以下输出 −
URI = https://www.tutorialspoint.com/ String representative = https://www.tutorialspoint.com/ URI = https://www.tutorialspoint.com/jquery.htm#abcd String representative = https://www.tutorialspoint.com/jquery.htm#abcd 两个 URI 不相等! Relative uri = jquery.htm#abcd
示例
现在让我们看一个实现 Console.Clear 方法的相同示例 −
using System;
public class Demo {
public static void Main(){
Uri newURI1 = new Uri("https://www.tutorialspoint.com/");
Console.WriteLine("URI = "+newURI1);
Console.WriteLine("String representation = "+newURI1.ToString());
Uri newURI2 = new Uri("https://www.tutorialspoint.com/jquery.htm#abcd");
Console.WriteLine("
URI = "+newURI2);
Console.WriteLine("String representation = "+newURI2.ToString());
if(newURI1.Equals(newURI2))
Console.WriteLine("
Both the URIs are equal!");
else
Console.WriteLine("
Both the URIs aren't equal!");
Uri res = newURI1.MakeRelativeUri(newURI2);
Console.WriteLine("Relative uri = "+res);
Console.Clear();
Console.WriteLine("Console cleared now!");
}
}
输出
这将产生以下输出 −
Console cleared now!

