Console in C# means console. Console is a class that encapsulates some basic operations of the console, such as [Console.Write], which means writing a string directly to the console.
C#'s Console
Console.Write means writing characters directly to the console String, no newline is performed, and the previous characters can be continued to be written.
Console.WriteLine means writing a string to the console and then wrapping it.
Console.Read means reading a string from the console without line breaks.
Console.ReadLine means to wrap the string after reading it from the console.
Console.ReadKey Gets the next character or function key pressed by the user. The pressed key is displayed in the console window.
Console.Beep Plays a beep through the console speaker.
Console.Clear clears the console buffer and the display information of the corresponding console window.
Output to console
Output to console means outputting data to the console and displaying it. The .Net framework provides the console class to implement this task. The output method is as follows:
Console.WriteLine();
Console.Write() ;
Console.WriteLine(output value);
Console.Write(output value);
Console.WriteLine("Output format string", variable list);
Console.Write("Output format string", variable list);
Console.WriteLine("This is {0}, this is {1} and {2}",strName[0],strName[1],strName [2],strName3]);
This method contains two parameters: "format string" and variable list. "This is {0}, this is {1} and {2}" This is the format string. {0}, {1}, {2} are called placeholders, representing the variable table arranged in sequence, and 0 corresponds to the variable The first variable in the list, 1, corresponds to the second variable in the variable list, and so on to complete the output.
Input from the console
Input from the console means inputting data from the console to the program.
Input method provided by the Console class:
Console.ReadLine();
This code returns a string type data, which can be directly assigned to a string variable , such as:
string strname=Console.ReadLine();
Sometimes you need to input numbers from the console, and you need to use the content introduced earlier, data conversion, such as:
int num=int.Parse(Console.ReadLine()); int num=Convert.ToInt32(Console.ReadLine());
The above two sentences of code have the same effect, you can use it according to your own habits Choose any one.
Note:
The input results of Console.ReadLine() and Console.Read() are completely different and cannot be mixed.
Console.Read(), The return value is the ASCII code of the first character
Console.ReadLine(), The return value is a string.
That is to say, the read method can only read the first character, while ReadLine can read multiple characters and read in new lines
Console.ReadKey() Function:
read is read from the console, key means pressing the keyboard, so the combination means to get the user to press the function key and display it in the window. The previous code is used to pause the window. Function, in debugging mode, the window will only close after pressing any key
.
Simple case:
using System; using System.Collections.Generic; using System.Linq;using System.Text; using System.Threading.Tasks;namespace ConsoleTest { class Program { static void Main(string[] args) { Console.WriteLine("输入用户名和ID"); string name = Console.ReadLine(); int id = int.Parse(Console.ReadLine()); Console.WriteLine("User Name is {0} \nThe id is {1}",name, id); Console.ReadKey(); } }
The above is the detailed content of What does console mean in C#. For more information, please follow other related articles on the PHP Chinese website!