Console.ReadLine() Method in c#
Namespace :- System
Assembly :- System.Console.dll
Signature :- public static string? ReadLine ();
When working with console applications in C#, you often need to take user input from the keyboard. The most common and straightforward way to do this is by using the Console.ReadLine() method.
The ReadLine method reads a line from the standard input stream and returns it as a string. The ReadLine method executes synchronously. it blocks until a line is read(input) or the Ctrl+Z keyboard combination is pressed.
In this article, we’ll dive deep into everything you need to know about Console.ReadLine() from basic usage to advanced behavior, exceptions, buffering and real-world scenarios.
What Console.ReadLine() Does
The Console.ReadLine() method reads a complete line of input from the standard input stream (stdin) until the Enter key is pressed or the end-of-input (Ctrl+Z) signal is received.
It executes synchronously that means the program pauses execution and waits for user input before continuing.
Return Value- Returns the line of characters entered by the user (without the newline characters).
- Returns null if the end of input is reached (for example, when Ctrl+Z is pressed).
Internal Working
When you call Console.ReadLine(), the method follows these internal steps
- Checks the input stream (Console.In) :- This stream is usually connected to the keyboard for user input.
- Reads characters one by one until it encounters a newline sequence (\r\n on Windows or \n on Unix systems).
- Stores the input temporarily in an internal buffer (typically 256 bytes).
- Removes the newline characters from the end of the input.
- Returns the complete string that was entered by the user.
- Handles EOF (End of File): If the user presses Ctrl+Z (Windows) or Ctrl+D (Linux/macOS), it immediately returns null, indicating no more input is available.
Example :-
using System.IO;
using System ;
class Program
{
static void Main()
{
Console.WriteLine(" Enter your Name : " );
string line = Console.ReadLine();
Console.WriteLine(line);
}
}// Console Output Enter your Name : c-sharp c-sharp
Exceptions Handling
1. IOException
Thrown when an I/O error occurs while reading from the standard input stream (Console.In). For example, if the standard input stream is unexpectedly closed, redirected or becomes unavailable.
2. OutOfMemoryException
Thrown when the system runs out of memory while allocating a buffer for the input line (very long input). In short : There is insufficient memory to allocate a buffer for the returned string.
For Example : If the user pastes an extremely large input line (hundreds of MBs).
3. ArgumentOutOfRangeException
The number of characters in the next line of characters is greater than Int32.MaxValue.
Console.Read() vs Console.ReadLine() vs Console.ReadKey()
| Method | Reads | Returns | Blocking | Typical Use |
|---|---|---|---|---|
Read() |
Single character | int (ASCII code) |
Yes | Low-level character reading |
ReadLine() |
Full line | string |
Yes | Most user input |
ReadKey() |
Single key press | ConsoleKeyInfo |
Yes | Detecting special keys (arrows, Esc etc.) |
Important Note :-
1. One of the most common uses of the ReadLine method is to pause program execution to prompt the user to press the Enter key before terminating the application.
2. If standard input is redirected to a file the ReadLine method reads a line of text from a file.
3. If the Ctrl+Z character is pressed when the method is reading input from the console the method returns null. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop.
using System;
public class Example
{
public static void Main()
{
string line;
Console.WriteLine("Ente text (press CTRL+Z to exit):");
do {
line = Console.ReadLine();
if (line != null)
Console.WriteLine(line);
} while (line != null);
}
}By default, the method reads input from a 256-character input buffer. Because this includes the Environment.NewLine characters the method can read lines that contain up to 254 characters.
If you expect longer input lines, you can extend the input buffer using OpenStandardInput method.
Stream inputStream = Console.OpenStandardInput(1024); Console.SetIn(new StreamReader(inputStream));
This allows you to handle inputs longer than the default buffer size.
5. If the standard input is redirected to a file or stream, ReadLine() will read text lines from that source instead of the keyboard.
dotnet run < input.txt
When accepting numeric input, you should handle invalid entries gracefully.
Exampleusing System;
class Program
{
static void Main()
{
Console.Write("Enter your age: ");
string? input = Console.ReadLine();
if (int.TryParse(input, out int age))
{
Console.WriteLine($"You are {age} years old.");
}
else
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
}This prevents runtime errors from FormatException when non-numeric text is entered.
Console.ReadLine() reads the entire line as one string. To split it into multiple parts (like words or numbers), use Split().
Console.WriteLine("Enter numbers separated by space:");
string? input = Console.ReadLine();
if (input != null)
{
var numbers = input.Split(' ');
foreach (var num in numbers)
Console.WriteLine(num);
}10 20 30
10 20 30
Summary
| Feature | Description |
|---|---|
| Purpose | Reads a line of input from console or redirected stream |
| Return Type | string? (returns null on EOF) |
| Default Buffer Size | 256 characters |
| Execution Mode | Blocking (synchronous) |
| Namespace | System |
| Assembly | System.Console.dll |
| Common Use | Accepting user input, pausing program execution |
