Difference between ReadLine(), Read(), ReadKey() in C#

As MSDN is actually pretty clear.

Console.ReadLine()

Reads the next line of characters from the standard input stream. simply you can say, it read all the characters from user input. (and finish when press enter).

Note: It return a STRING so data type should be STRING.

Example

Console.ReadLine

When i enter a string "This is ReadLine" it read as it is. its mean it reads all characters until the end of line.

Console.Read()

Reads the next character from the standard input stream. it only accept single character from user input and return its ASCII Code.

Note: Data type should be int. because it return an integer value as ASCII.

Example

Console.Read

AsI input "6" and it return its ASCII value "54".

Console.ReadKey()

It obtains the next character or function key pressed by the user. In simple words, it read that which key is pressed by user and return its name. it does not require to press enter key before entering.

Note: It is STRUCT Data type which is ConsoleKeyInfo.

Example:

Code

As I press different Buttons from keyboard like Escape, Spacebar, Enter, R, P, Z, Minus and it return name.

Ebook Download
View all
Learn
View all