Skip to content
Tutorials Center
My learning

Topics

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().

C#
string? line = Console.ReadLine();
if (line is null)
{
    Console.WriteLine("No input.");
}

Try it

C# Reading and parsing numbers

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.

C# Read, ReadToEnd and end of input

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

C#
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 to int.Parse directly. 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) returns false instead of throwing FormatException. For decimal numbers pass CultureInfo.InvariantCulture when you need the same behavior on every machine.
  • Empty line is not end of input. An empty line gives ""; only null means the stream is over.
  • Trim when needed. ReadLine keeps leading and trailing spaces. Call Trim() before comparing commands.
  • Prompt first. Use Console.Write("Name: ") (no newline) before ReadLine(); 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 through Console.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.