C# Reference ยท Files and I/O
Console.ReadLine() and reading input
Reference for Console.ReadLine, Read and ReadKey in C#: reading lines until end of input, parsing numbers safely, null results and redirected input.
Console.ReadLine() reads one line of text from standard input and returns it without the line terminator. It blocks until the user presses Enter or the input ends. When there is no more input (the user pressed Ctrl+D or Ctrl+Z, or a redirected file or pipe is exhausted) it returns null, which is why its return type is string?.
Related methods read at a finer grain: Console.Read() returns a single character code (or -1 at the end), and Console.ReadKey() waits for one key press, which only works on an interactive terminal. For all input from a script or test harness, stick to ReadLine, Read and Console.In.ReadToEnd().
string? line = Console.ReadLine(); if (line is null) { Console.WriteLine("No input."); }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static string? ReadLine(); public static int Read(); public static ConsoleKeyInfo ReadKey(); public static ConsoleKeyInfo ReadKey(bool intercept); public static bool KeyAvailable { get; } // Console.In is a TextReader: Peek(), ReadToEnd(), ReadLineAsync(), ReadAsync(...)
| Name | Description |
|---|---|
ReadLine() |
Reads up to the next line break (\n, \r\n) and returns the text without it. |
Read() |
Returns the next UTF-16 code unit as an int, or -1 at end of input. On a terminal it returns after Enter. |
ReadKey(intercept) |
Waits for a key and returns a ConsoleKeyInfo (Key, KeyChar, Modifiers). With intercept: true the key is not echoed. |
KeyAvailable |
true if a key press is waiting. Never blocks. |
Returns: string? that is null at end of input; int that is -1 at end of input.
Errors: InvalidOperationException from ReadKey and KeyAvailable when input is redirected or there is no console. OutOfMemoryException is possible if a single line is enormous, and IOException if standard input is closed unexpectedly.
Usage notes
- Always handle
null. With nullable reference types enabled the compiler warns if you pass the result toint.Parsedirectly. Use?? "", a null check, or a loop condition like(line = Console.ReadLine()) is not null. - Parse with
TryParse. User input is untrusted.int.TryParse(line, out var n)returnsfalseinstead of throwingFormatException. For decimal numbers passCultureInfo.InvariantCulturewhen you need the same behavior on every machine. - Empty line is not end of input. An empty line gives
""; onlynullmeans the stream is over. - Trim when needed.
ReadLinekeeps leading and trailing spaces. CallTrim()before comparing commands. - Prompt first. Use
Console.Write("Name: ")(no newline) beforeReadLine(); output is flushed immediately, so the prompt appears right away. - Splitting numbers on one line.
Console.ReadLine()!.Split(' ', StringSplitOptions.RemoveEmptyEntries).Select(int.Parse)is a common pattern for competitive-programming style input. - Large input. For many lines it is faster to read via
new StreamReader(Console.OpenStandardInput())than throughConsole.In. - Async.
await Console.In.ReadLineAsync()avoids blocking the calling thread, although on most platforms the underlying read is still synchronous. - Interactive passwords.
ReadKey(true)in a loop lets you hide typed characters, but only on a real terminal.