Difference between Console.Read() and Console.ReadLine()?
Asked Answered
U

12

38

I'm new to this field and I'm very confused: what is the real difference between Console.Read() and Console.ReadLine()?

Uncinariasis answered 26/7, 2011 at 6:5 Comment(3)
Quick googling: Console.Read reads a single character, and Console.ReadLine reads a whole line.Disentomb
A quick check of the online MSDN would answer that: A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or the value of the Environment.NewLine property. The returned string does not contain the terminating character(s).Acolyte
Also worth reading - Console.Read() and Console.ReadLine() problemsTurrell
O
43

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.

Obel answered 26/7, 2011 at 6:7 Comment(0)
M
18

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).

Multifid answered 9/1, 2017 at 7:49 Comment(1)
clearly described maybe but ReadKey does not return a character - it returns an instance of ConsoleKeyInfo that you would need to pull the .Key property from (as it also includes metadata like whether the shift key was held)Related
S
9

Console.Read() reads just a single character, while Console.ReadLine() reads all characters until the end of line.

Simoniac answered 26/7, 2011 at 6:8 Comment(0)
C
5

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.
Citron answered 26/7, 2011 at 6:7 Comment(0)
E
4

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.

Entrain answered 9/8, 2014 at 11:33 Comment(1)
The character set is probably not ASCII. ASCII hasn't been used much in consoles in any MS operating system (including MS-DOS). It's a user preference and ASCII is not the default. The behavior of Console.Read depends on Console.InputEncoding.Trussell
O
2

Console.Read() reads a single key, where Console.Readline() waits for the Enter key.

Oaten answered 26/7, 2011 at 6:7 Comment(0)
M
2

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.

Mightily answered 3/9, 2013 at 16:19 Comment(1)
And what about Console.ReadKey() ?Inflexible
G
1

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();
Goldshell answered 22/6, 2020 at 18:18 Comment(0)
S
0

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.

Spokesman answered 29/1, 2015 at 9:24 Comment(0)
U
0

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:

  1. These three methods are mainly used in Console application and these are used for returning different values.
  2. If we use Read line or Read(), we need press Enter to come back to code.
  3. If we using Read key(), we can press any key to come back code in application.
Undertaking answered 17/11, 2015 at 10:11 Comment(1)
'Read()' reads first character from input stream and returns ASCII value of the corresponding character. 'ReadKey()' reads character from input stream and returns struct ConsoleKeyInfo which contains 'KeyChar', 'Key' and 'Modifiers' for information like which key is pressed and whether there is any ALT, Shift, Ctrl pressed while entering the input or not. 'ReadLine()' returns the entered string from input console as stringPatricide
M
-1

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().

Mcgovern answered 20/2, 2012 at 12:7 Comment(1)
Your answer doesn't really add anything compared to the other answers (which were all written within a minute of each other 7 months ago, which is why they are so similar)...Raymond
P
-3
Console.Read()

=> reads only one character from the standard input

Console.ReadLine()

=> reads all characters in the line from the standard input

Pisci answered 15/11, 2016 at 11:45 Comment(1)
How is this answer of additional value over the other answers?Banyan

© 2022 - 2024 — McMap. All rights reserved.