Specific usage of the C Console class

  • 2020-05-10 18:39:13
  • OfStack

Console.Write means to write the string directly to the console, without breaking the line, and to continue with the previous character.
Console.WriteLine means write a string to the console and wrap it.
Console.Read means to read a string from the console without wrapping it.
Console.ReadLine means to wrap a string after reading it from the console.
Console.ReadKey gets the next character or function key pressed by the user, which is displayed in the console window.
Console.Beep plays the cue sound through the console speaker.
Console.Clear clears the console buffer and the display information of the corresponding console window.

Output to console

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

Console.WriteLine();
Console.Write();
Console.WriteLine(output value);
Console.Write (output value);
Console.WriteLine (" output format string ", list of variables);
Console.Write (" output format string ", list of variables);

The only 1 of Console.WrietLine () and Console.Write () are the ones that feed the output of the former and not the other way around.
Console. WriteLine (" the deer and the cauldron in the wife have {1} {0}, {2}, {3} etc. 7 ", strName [0], strName [1], strName

[2],strName3]);
This approach takes two arguments: a "format string" and a list of variables. "The wife of {0} in the deer and the cauldron has {1},{2},{3}, etc. 7" this is the format string,{0},{1},{2},{3} are called placeholders, representing the variable list in sequence, 0 corresponds to the first variable in the list of variables, 1 corresponds to the second variable in the list of variables, and so on to complete the output.

Enter from the console

The input method provided by the Console class:

Console.ReadLine();

This code returns a string of data, which can be directly assigned to a string variable, such as:
string strname=Console.ReadLine();
Sometimes it is necessary to input Numbers from the console, using the previous content, data conversion, such as:
int num=int.Pares(Console.ReadLine());
int num=Convert.ToInt32(Console.ReadLine());
The above two lines of code have the same effect, and you can choose any one of them according to your habits.

Note:      

The input results of   Console.ReadLine () and Console.Read () are completely different and cannot be mixed.
  Console.Read (), returns the ASCII code for the first character
  Console.ReadLine (), the return value is a string
This means that the read method can only read the first character, while ReadLine can read multiple characters and can also read   by wrapping lines

Console.ReadKey (), read is read from the console, key means to press the keyboard, then the combination in 1 means to get the user to press the function key to display in the window, and the code in front plays the function of window suspension. In the debugging state, only pressing any key will close the rear window.

Console I/o


using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;
namespace ConsoleTest
{
class ConsoleTest
{
static void Main(string[] args)
{
Console.WriteLine(" Please enter the names of two students ");
string name1=Console.ReadLine();
string name2=Console.ReadLine();
Console.WriteLine(" Please enter the scores of two students ");
int score1=int.Parse(Console.ReadLine());
int score2=int.Parse(Console.ReadLine());
Console.WriteLine(" The first 1 Student's name {0}, results {1}",name1,score1);
Console.WriteLine(" The first 2 Student's name {0}, results {1}",name2,score2);
Console.ReadKey();
}
}
}


Related articles: