Javatpoint Logo
Javatpoint Logo

Difference between Console.Read and Console.ReadLine in C#

The "Console.Read()" and "Console.ReadLine()" methods in C# are used to accept input from a standard input device. Within the System namespace, the Console is a predefined class. However, both Read() and ReadLine() are Console Class methods. In this article, you will learn about the difference between the Console.Read() and Console.Readline() function in C#. But before discussing their difference, you must know about the Console.Read() and Console.Readline() in C#.

What is the Console.Read() method?

The method "Console.Read()" takes the next character from the standard input stream, usually the keyboard, and returns its Unicode value as an integer. After reading just one character, the input cursor moves to the next character in the input stream. It can handle every Unicode character since it reads characters as Unicode code points and returns an integer.

It returns '-1', which means that the input stream is at the end.

Syntax:

It has the following syntax:

Return Value:

  • If there are no more characters to be read at this time, it returns a negative value (-1). Otherwise, it returns the character that follows from the input stream.

Exception:

  • Exception should an I/O error occur, this method will throw an IOException.

Note: The data type should be int because it returns an integer value in ASCII.

Example:

Let us take an example to illustrate the Console.Read() method in C#.

Output:

Please enter the character:
M
Your entered character is: M
Its Unicode value is: 77

Explanation:

In summary, this program asks the user to enter a character, reads the input from the Console as an integer that represents the character's Unicode value, converts the input to a character, and then results in the character that the user entered and its Unicode value back to the Console. If the input stream reaches its end, it notifies the user accordingly.

What is the Console.ReadLine ?

  • The method "Console.ReadLine()" reads the next line of character from the standard input stream and returns it as a string.
  • It continues reading all characters until the end of the input stream or a newline character ('\n').
  • It returns the input as a string, which means it preserves the whitespace and any characters until the end of the line.

Note: The data type must also be STRING because a STRING is returned.

Syntax:

It has the following syntax:

Return Value :

The next line of characters of the string type from the input stream is returned or null if no more lines are available.

Example:

Let us take an example to illustrate the Console.ReadLine() method in C#.

Output:

Please enter a line of text:
Hello World!
You entered String is: Hello World!

Explanation:

In summary, this program prompts the user to enter a line of text, reads the input from the Console, and then prints the entered text back to the Console with a message indicating what was entered.

Main differences between Console.Read and Console.ReadLine

There are several main differences between the Console.read() and Console.ReadLine() method in C#. Some main differences are as follows:

Feature Console.Read() Console.ReadLine()
Data Type int string
Purpose A single character is read from the input stream. Reads an entire line of text from the input stream.
Return Type Returns an integer that is the character's Unicode value. Returns a string representing the line of text entered.
Input Processing Only reads the input stream's next character. Until a newline character is encountered or the stream's end reads every character.
Removes Newline No Yes
Input Buffer Don't wait for the Enter key; read immediately. Waits for Enter key; reads after the user presses Enter.
Return Value int (Unicode value of the character) string (line of characters)
End of Input If the input stream ends, it returns -1. If the input ends, it returns an empty string.
Blocking Behavior Blocks until a character is available Blocks until a line is entered
Example int num_ber = Console.Read(); string str = Console.ReadLine();

In C#, "Console.Read" and "Console.ReadLine" differ mainly in the following table. It focuses on how each method handles input buffer management, blocking behaviour, newline character handling, input and return types.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA