SoFunction
Updated on 2025-03-07

Specific usage of C# Console class

It means that the string is written directly to the console without wrapping the line, and the previous characters can be continued.
Indicates that a string is written to the console and a new line is broken.
Indicates that strings are read from the console without wrapping lines.
Indicates that a string is read from the console and then a new line is opened.
Gets the next character or function key pressed by the user, and the pressed key is displayed in the console window.
Play prompt tones through the console speaker.
Clear the display information of the console buffer and the corresponding console window.

Output to console

Output to the console is to output the data to the console and display it. The .Net framework provides the console class to implement this task, and the output method is as follows:

();
();
(Output value);
(Output value);
("Output format string", variable list);
("Output format string", variable list);

The only difference between () and () is that the former outputs the line break after the line break, while the latter does not.
("The wife of {0} in the Deer and the Cauldron has 7 such as {1}, {2}, {3}", strName[0], strName[1], strName

[2],strName3]);
This method contains two parameters: "Format string" and variable list. "The wife of {0} in the Deer and the Cauldron has 7 {1}, {2}, {3}, etc." This is a format string. {0}, {1}, {2}, {3} is called placeholders, representing the variable table arranged in sequence afterwards. 0 corresponds to the first variable list, 1 corresponds to the second variable list, and so on, the output is completed.

Enter from the console

The input methods provided by the Console class:

();

This code returns a string-type data, which can be assigned directly to string variables, such as:
string strname=();
Sometimes you need to enter numbers from the console, and use the content introduced above, data conversion, such as:
int num=(());
int num=Convert.ToInt32(());
The above two sentences have the same effect, and you can choose any one according to your habits.

Notice:

The input results of () and () are completely different and cannot be mixed.
(), return ASCII code with the first character
(), the return value is a string
In other words, the read method can only read the first character, while ReadLine can read multiple characters or read newlines.
 

The function of () is to read from the console, and the key means to press the keyboard. The combination means to obtain the function key displayed in the window by the user pressing the function key, and the function of the window pause is used in the previous code. In the debugging state, the window will only be closed after pressing any key.

Console input and output

Copy the codeThe code is as follows:

using System;
using ;
using ;
using ;
namespace ConsoleTest
{
class ConsoleTest
{
static void Main(string[] args)
{
("Please enter the names of two students");
string name1=();
string name2=();
("Please enter the grades of two students");
int score1=(());
int score2=(());
("The first student's name {0}, score {1}", name1, score1);
("The second student's name {0}, score {1}", name2, score2);
();
}
}
}