I'm new to this field and I'm very confused: what is the real difference between Console.Read()
and Console.ReadLine()
?
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.
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).
ConsoleKeyInfo
that you would need to pull the .Key
property from (as it also includes metadata like whether the shift key was held) –
Related Console.Read()
reads just a single character, while Console.ReadLine()
reads all characters until the end of line.
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.
The basic difference is:
int i = Console.Read();
Console.WriteLine(i);
paste above code and give input 'c', and the output will be 99. That is Console.Read give int value but that value will be the ASCII value of that..
On the other side..
string s = Console.ReadLine();
Console.WriteLine(s);
It gives the string as it is given in the input stream.
Console.Read()
reads a single key, where Console.Readline()
waits for the Enter key.
Console.Read()
basically reads a character so if you are on a console and you press a key then the console will close, meanwhile Console.Readline()
will read the whole string.
Console.Read()
- It only accepts a single character from user input and returns its ASCII Code.
- The data type should be int. because it returns an integer value as ASCII.
- ex -> int value = Console.Read();
Console.ReadLine()
- It read all the characters from user input. (and finish when press enter).
- It returns a String so data type should be String.
- ex -> string value = Console.ReadLine();
Console.ReadKey()
- It read that which key is pressed by the user and returns its name. it does not require to press enter key before entering.
- It is a Struct Data type which is ConsoleKeyInfo.
- ex -> ConsoleKeyInfo key = Console.ReadKey();
The difference of Read(),ReadLine() and Readkey() method are given below:
Read():This is static method in Console class:
int i = Console.Read();//it always return int value.
Console.WriteLine(i);
paste above code and give input '1', and the output will be 49. That is Console.Read give int value but that value will be the ASCII value of that.
ReadLine():
string s= Console.ReadLine();//it always return string value.
Console.WriteLine(s);
It gives the string as it is given in the input stream.
ReadKey(): this method is used to hold the output screen.when any key is press. Read() and ReadLine() is used the enter key for exit.
Difference between Read(), Readline() and ReadKey() in C#:
Read()
- Accept the string value and return the string value.Readline()
- Accept the string and return Integer.ReadKey()
- Accept the character and return Character.
Summary:
- These three methods are mainly used in Console application and these are used for returning different values.
- If we use Read line or Read(), we need press Enter to come back to code.
- If we using Read key(), we can press any key to come back code in application.
Console.Read()
is used to read next charater from the standard input stream.
When we want to read only the single character then use Console.Read()
.
Console.ReadLine()
is used to read aline of characters from the standard input stream.
when we want to read a line of characters use Console.ReadLine()
.
Console.Read()
=> reads only one character from the standard input
Console.ReadLine()
=> reads all characters in the line from the standard input
© 2022 - 2024 — McMap. All rights reserved.
Console.Read
reads a single character, andConsole.ReadLine
reads a whole line. – Disentomb