Some misunderstandings about the Console. Read () method!

Source: Internet
Author: User
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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.