Clear console output using Console.Clear function in C#

WBOY
Release: 2023-11-18 11:00:51
Original
1422 people have browsed it

Clear console output using Console.Clear function in C#

Use the Console.Clear function in C# to clear the console output

In C# console applications, we often need to clear the output information in the console so that To display new content or provide a better user experience. C# provides the Console.Clear function to implement this function, which can clear the output in the console and make the interface blank again.

The calling format of the Console.Clear function is as follows:

Console.Clear();
Copy after login

This function does not require inputting any parameters and can be called directly to clear the output in the console. Let's show you how to use the Console.Clear function to clear the console output.

First, we create a console application and import the System namespace so that we can use the related functions of the Console class. In the Main function, we first output some text information, then call the Console.Clear function to clear it, and finally output some new text information. The complete code is as follows:

using System;

namespace ConsoleClearDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("这是要清空的文本信息1");
            Console.WriteLine("这是要清空的文本信息2");
            Console.WriteLine("这是要清空的文本信息3");
            Console.WriteLine("这是要清空的文本信息4");

            Console.WriteLine("按任意键清空控制台输出...");
            Console.ReadKey();

            Console.Clear();

            Console.WriteLine("控制台输出已经清空,这是新的文本信息5");
            Console.WriteLine("控制台输出已经清空,这是新的文本信息6");

            Console.WriteLine("按任意键退出程序...");
            Console.ReadKey();
        }
    }
}
Copy after login

In the above code, we first output some text information, and then wait for the user to enter any key. When the user presses any key, the console calls the Console.Clear function to clear it, then outputs some new text information, and finally waits for the user to enter any key again to exit the program.

When running the above program, you will find that all the output in the console has been cleared, leaving only the last new text message output. This is what the Console.Clear function does to clear the console output.

To summarize, using the Console.Clear function in C# can easily clear the console output and make the interface blank again. I hope the sample code in this article can help you and enable you to better use the C# language to develop console applications.

The above is the detailed content of Clear console output using Console.Clear function in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!