This method is used to read the next line of characters from the standard input stream. It comes under the Console class(System Namespace). If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key. And if standard input is redirected to a file, then this method reads a line of text from a file.
Syntax: public static string ReadLine ();
Return Value: It returns the next line of characters of string type from the input stream, or null if no more lines are available.
Exceptions:
- IOException: If an I/O error occurred.
- OutOfMemoryException: If there is insufficient memory to allocate a buffer for the returned string.
- ArgumentOutOfRangeException: If the number of characters in the next line of characters is greater than MaxValue.
Below program illustrate the use of the above-discussed method:
Example 1: Here, take input from the user. Since age is an integer, we typecasted it using Convert.ToInt32() Method. It reads the next line from the input stream. It blocks until Enter key is pressed. Hence it is commonly used to pause the console so that the user can check the output.
csharp
using System;
using System.IO;
class GFG {
public static void Main()
{
int age;
string name;
Console.WriteLine("Enter your name: ");
name = Console.ReadLine();
Console.WriteLine("Enter your age: ");
age = Convert.ToInt32(Console.ReadLine());
if (age >= 18)
{
Console.WriteLine("Hello " + name + "!"
+ " You can vote");
}
else {
Console.WriteLine("Hello " + name + "!"
+ " Sorry you can't vote");
}
}
}
|
Output:

Example 2: To pause the console
csharp
using System;
using System.IO;
class Geeks {
public static void Main()
{
string name;
int n;
Console.WriteLine("Enter your name: ");
name = Console.ReadLine();
Console.WriteLine("Hello " + name +
" Welcome to GeeksforGeeks!");
Console.ReadLine();
}
}
|
Output:

Explanation: In the above output you can see that the console is paused. The cursor will blink continuously until you press Enter key.
Reference:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
26 Aug, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...