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#
// C# program to show the difference // between Console.Read() and // Console.ReadLine() method using System; public class GFG{ static void Main( string [] args) { // use of Read() method 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#
// C# program to show the difference // between Console.Read() and // Console.ReadLine() method using System; public class GFG{ static void Main( string [] args) { // use of ReadLine() method 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.