Some beginners often misunderstand the Console. Read () method! What are the specific misunderstandings? Let's first look at a piece of code:
Static void Main (string [] args)
{
Int i1 = 123;
Console. WriteLine (d1 );
Console. Read ();
Int i2 = 456;
Console. WriteLine (_ i2 );
Console. Read (); // The program is not blocked.
Console. WriteLine ("abc ");
Console. Read ();
}
We often think that the Console. the Read () method can block the running of the program, but this is not the case. The second call to the Read method does not block the running of the program, which is mainly related to the processing method of the Read method, when the Read method is executed for the first time, blocking will occur. When you press Enter, blocking is removed and a termination sequence will be added to the input content (carriage return and line feed in Windows) at this time, the incoming and outgoing content is only (\ r \ n), and the Read method retrieves the carriage return (\ r ).
Then execute the second call to the Read method. The Read method called this time retrieves the line break (\ n ). Note that this is the last character.
The third time you call the Read method, this is because all the characters in the input content have been retrieved. Therefore, the Read method will re-block your program. If there is a call to the Read method in the future, this is a new cycle. Repeat the search steps I mentioned above!
The Read method is as follows:
The first call will block your program until you Enter, and a termination sequence will be added to the input content (carriage return and line break in Windows)
Subsequent Read method calls will not block your program until the last character is retrieved by the Read method. If you want to call the Read method again, the program will be blocked again, just like calling the Read method for the first time, but this is a new cycle!