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

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#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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()));
    }
}

chevron_right


Input:

Geeks

Output:



Gee

Program 2: Example of Console.ReadLine() in C#.

C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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());
    }
}

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.