Difference between Console.Read and Console.ReadLine in C#
Last Updated :
05 Apr, 2022
In C#, to take input from the standard input device, the following method are used – Console.Read() and Console.ReadLine() method. Console is a predefined class of System namespace. While Read() and ReadLine() both are the Console Class methods.
The only difference between the Read() and ReadLine() is that Console.Read is used to read only single character from the standard output device, while Console.ReadLine is used to read a line or string from the standard output device.
Program 1: Example of Console.Read() in C#.
C#
using System;
public class GFG{
static void Main( string [] args)
{
Console.Write(Convert.ToChar(Console.Read()));
Console.Write(Convert.ToChar(Console.Read()));
Console.Write(Convert.ToChar(Console.Read()));
}
}
|
Input:
Geeks
Output:
Gee
Program 2: Example of Console.ReadLine() in C#.
C#
using System;
public class GFG{
static void Main( string [] args)
{
Console.Write(Console.ReadLine());
Console.Write(Console.ReadLine());
Console.Write(Console.ReadLine());
}
}
|
Input:
Geeks
For
Geeks
Output:
GeeksForGeeks
In the above code, program 1 shows that it will read only single character and program 2 shows it will read string until new line character is not found.
Let us see the differences in Tabular Form -:
|
Console.read() |
Console.readline() |
|
Console.Read() is a method that is used to read the next character from the standard input stream |
Console.readline() is a method that is used to read the next line of characters from the standard input stream |
|
Its syntax is -:
public static int Read ();
|
Its syntax is -:
public static string ReadLine ();
|
|
Its return value is character |
Its return value is multiple characters as it returns a whole new line |
|
If there is no next character Present then it returns -1 |
If there is no line present it returns NULL |
|
We cannot use it to read Multiple characters at a time |
We cannot use it to read Multiple characters at a time |
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...