C# Console ReadLine() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Console

Definition

In C#, Console.ReadLine reads characters from standard input until the user presses Enter (or the line ends). It returns the line as a string without 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() ?? "";
CallWhat 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

ParameterDescription
NoneReadLine() takes no parameters.

Return Value

Return ValueDescription
stringThe line of text without the ending newline.
nullEnd 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 + "!");
    }
}

How It Works

  1. Prompt with Write. The question and answer share one visual line.
  2. Sample input. Output narrative shows typing Maya then Enter.
  3. Null coalesce. ?? "friend" substitutes a default if EOF yields null.
  4. 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.");
        }
    }
}

How It Works

  1. Text first. ReadLine always gives a string (or null), never an int directly.
  2. TryParse. Avoids exceptions when the user types letters.
  3. Sample 20. sample output shows a valid age being entered.
  4. 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");
    }
}

How It Works

  1. Interactive loop. Each iteration waits for another line.
  2. Echo sample. Output shows hello, then quit to exit.
  3. Two stop cases. Null (EOF) or the word quit both break.
  4. 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).

Next: ResetColor()

Restore foreground and background console colors to their defaults with System.Console.

ResetColor() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful