In C#, Console.ReadLine reads characters from standard input until the user presses Enter (or the line ends). It returns the line as a stringwithout the final newline characters.
At end of file, ReadLine returns null. Always decide what to do with null—use ?? "", check explicitly, or exit. For a single key, use ReadKey; for raw character codes, use Read.
This is a static member of System.Console. You call it with the type name, such as Console.ReadLine(...). Console is the everyday API for reading from standard input and writing to standard output in C# console apps.
Syntax
Store the result in a string and handle a possible null.
C#
string? line = Console.ReadLine();
string name = Console.ReadLine() ?? "";
Call
What it does
Console.ReadLine()
Next line as string, or null at EOF.
The returned string does not include the trailing newline. Empty input (just Enter) returns "".
Library declarations
C#
public static string? ReadLine();
Parameters
Parameter
Description
None
ReadLine() takes no parameters.
Return Value
Return Value
Description
string
The line of text without the ending newline.
null
End of input (EOF).
""
User pressed Enter with no characters.
C# Examples
Example 1 — Read a Name and Greet
Classic beginner pattern: ask, read a line, print a greeting.
C#
using System;
class Program {
static void Main() {
Console.Write("What is your name? ");
string name = Console.ReadLine() ?? "friend";
Console.WriteLine("Nice to meet you, " + name + "!");
}
}
Output
What is your name? Maya
Nice to meet you, Maya!
How It Works
Prompt with Write. The question and answer share one visual line.
Sample input. Output narrative shows typing Maya then Enter.
Null coalesce.?? "friend" substitutes a default if EOF yields null.
Greeting. WriteLine prints the full reply on the next line.
Example 2 — Read a Line and Parse an Int
Read text, then convert with int.TryParse for safety.
C#
using System;
class Program {
static void Main() {
Console.Write("Enter age: ");
string? text = Console.ReadLine();
if (text != null && int.TryParse(text, out int age)) {
Console.WriteLine("Next year you will be " + (age + 1));
} else {
Console.WriteLine("Please enter a whole number.");
}
}
}
Output
Enter age: 20
Next year you will be 21
How It Works
Text first. ReadLine always gives a string (or null), never an int directly.
TryParse. Avoids exceptions when the user types letters.
Sample 20. sample output shows a valid age being entered.
Null check. Skips parsing when the stream has already ended.
Example 3 — Read Lines Until the User Types quit
Keep reading commands in a loop.
C#
using System;
class Program {
static void Main() {
Console.WriteLine("Type commands (quit to stop):");
while (true) {
Console.Write("> ");
string? cmd = Console.ReadLine();
if (cmd == null || cmd == "quit") break;
Console.WriteLine("You said: " + cmd);
}
Console.WriteLine("Bye");
}
}
Output
Type commands (quit to stop):
> hello
You said: hello
> quit
Bye
How It Works
Interactive loop. Each iteration waits for another line.
Echo sample. Output shows hello, then quit to exit.
Two stop cases. Null (EOF) or the word quit both break.
Prompt marker.> from Write keeps the UI clear.
Frequently Asked Questions
null. Empty Enter returns "" (empty string), which is different.
No. The line-ending characters are removed.
Read a line as text, then parse with int.TryParse or Convert methods.
Write keeps the cursor on the same line as the question; WriteLine moves below.
ReadLine waits for a full line. ReadKey reads one key press (optionally without echo).