Difference between Console.Read (); and Console.ReadLine ();

7

I'm starting to learn C # and got a little confused by one thing.

What is the real difference between Console.Read(); and Console.ReadLine(); ?

    
asked by anonymous 16.02.2015 / 23:37

3 answers

8

The first reads a buffer character and the second reads a line, that is, all characters until it finds an end-of-line indicator. >

By reading only one character the Read() returns an integer indicating which character would be read. It's not even a% of%. A conversion is required if you need the information as a character. And of course if you need string the conversion is also required. Obviously% w /% returns a string to accommodate the entire line. The end-of-line signal is not part of this resulting string .

This buffer is usually the keyboard. There is no guarantee that it will come from there but the default input used by the console is this device. Obviously it may have been redirected but C # or .Net does not need to know this. Therefore, the end-of-line indicator is typically generated when ENTER is triggered. Technically speaking is when you receive a string char .

Source code for ReadLine() and NewLine . You need to go into other parts of the code to understand everything, but it's a start.

    
16.02.2015 / 23:43
1

Basically, you'll want to use Console.Read () when you want to return the ASCII VALUE of the first character you typed.

If you only want one character, you will have to do some checks, as no value will return.

Then:

  • Console.ReadKey () Returns the value of the key pressed
  • Console.ReadLine () Returns the value of the entire line
  • Console.Read () Returns the ASCII value of the first character entered on the line
04.03.2016 / 15:41
0

I just understood this in practice where a variable of my code (int) would receive via keyboard, for example: 240 and later multiplied that variable with another, but the end result of multiplication always went wrong. Testing the variable output it displayed 50 instead of 240.

Read () blame; Solution given by a friend here in stack

variável = Convert.Toint32(Console.Readline());

    
26.10.2017 / 21:44