C# C onsole. Read (); and C onsole. ReadLine (); and C onsole. ReadKey (); the difference is detailed.

Keywords: C# ascii

Console.Read(); and Console.ReadLine(); and Console.ReadKey(); the difference is detailed.

Console.Read(); Professional: Read the next character from the standard input stream. Popular: Read the first character and the second character entered by the keyboard, and so on, return the ASCII value, return and exit.

Sample code:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test002
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("Please enter more than one character.");
14             int readFirst = Console.Read();
15             Console.WriteLine("The value of the first input is:" + readFirst);
16             int readSecond = Console.Read();
17             Console.WriteLine("The value of the second input is:" + readSecond);
18 
19             Console.ReadKey();
20         }
21     }
22 }

Example results:

Console.ReadLine(); Professional: Read the next line of characters from the standard input stream. Popular: Read all characters, return string, return to exit.

Sample code:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test002
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.WriteLine("Please enter more than one character.");
14             string readLineFirst = Console.ReadLine();
15             Console.WriteLine("The value of the first input is:" + readLineFirst);
16             Console.WriteLine("Please enter more than one character.");
17             string readLineSecond = Console.ReadLine();
18             Console.WriteLine("The value of the second input is:" + readLineSecond);
19 
20             Console.ReadKey();
21         }
22     }
23 }

Example results:

Console.ReadKey(); Professional: Get the next character or function key pressed by the user. The keys pressed are displayed in the console window. Popular: Listening for keyboard events can be understood as pressing any key to execute.

Sample code:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace Test002
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Console.ReadKey();
14         }
15     }
16 }

Example results:

The above content is my own understanding, I hope to help friends in need.

Posted by courtewing on Sun, 20 Jan 2019 09:42:13 -0800