Difference between Console.Read() and Console.ReadLine()?
I'm new to this field and I'm very confused: what is the real difference between Console.Read()
and Console.ReadLine()
?
Solution 1:
Console.Read()
reads only the next character from standard input,
and Console.ReadLine()
reads the next line of characters from the standard input stream.
Standard input in case of Console Application is input from the user typed words in console UI of your application. Try to create it by Visual studio, and see by yourself.
Solution 2:
These are the methods of system.console
- ReadKey() (returns a character): reads only one single character from the standard input stream or command line. Usually used when you're giving options to the user in the console to select from, such as select A, B or C. Another prominent example, Press Y or n to continue.
- ReadLine() (returns a string): or Console.Readline() reads a single line from the standard input stream or the command line. As an example, it can be used to ask the user enter their name or age. It reads all the character till we press enter.
- Read() (returns an int): or Console.Read() reads only one single character from the standard input stream. Similar to ReadKey except that it returns an integer. It returns the next character from the input stream, or returns (-1) if there is no more character to be read.
(There are more system.console methods like write() and writeline() as well which are used to write in command line, behaving similarly as read() and readline() methods)
This was clearly described with examples in the MSDN documentation (links are included above).
Solution 3:
Console.Read()
reads just a single character, while Console.ReadLine()
reads all characters until the end of line.
Solution 4:
MSDN is actually pretty clear on this one.
- Console.Read: Reads the next character from the standard input stream.
- Console.ReadLine: Reads the next line of characters from the standard input stream.