User Input

Console programs read what the user types with Console.ReadLine. Input always arrives as text, so you'll combine it with the parsing techniques from lesson 9 — and because users type unexpected things, you'll check the input before trusting it.

Reading a line

Console.ReadLine() waits until the user presses Enter and returns what they typed, without the line break:

C#
Console.Write("What's your name? ");
string? name = Console.ReadLine();
Console.WriteLine($"Nice to meet you, {name}!");
Input
Mira
Output
What's your name? Nice to meet you, Mira!

The examples in this course show input in an Input box. In a real terminal you'd also see your own typing, so the output would appear as What's your name? Mira followed by the greeting on the next line.

Why string? (with a question mark)

ReadLine returns null when there's no more input — for example if input is redirected from a file that has ended, or the user presses Ctrl+D (Ctrl+Z on Windows). Its return type is string?, a string that might be null. If you treat it as definitely not null, the compiler warns you:

C#
Console.Write("City: ");
string city = Console.ReadLine();
Console.WriteLine(city.ToUpper());
Input
Oslo
Output
Program.cs(2,15): warning CS8600: Converting null literal or possible null value to non-nullable type.
Program.cs(3,19): warning CS8602: Dereference of a possibly null reference.
City: OSLO

The simplest fix is to replace a missing line with an empty string using ??:

C#
Console.Write("City: ");
string city = Console.ReadLine() ?? "";
Console.WriteLine(city.Trim().ToUpper());
Input
  Oslo
Output
City: OSLO

Trim() removes accidental spaces from the start and end — always a good idea with typed input.

Reading numbers

Parse the text into a number. Because users make typos, prefer TryParse over Parse:

C#
Console.Write("How many tickets? ");
string? text = Console.ReadLine();

if (int.TryParse(text, out int tickets) && tickets > 0)
{
    decimal cost = tickets * 12.50m;
    Console.WriteLine($"{tickets} tickets cost {cost:C}");
}
else
{
    Console.WriteLine("Please enter a positive whole number.");
}
Input
3
Output
How many tickets? 3 tickets cost $37.50

{cost:C} formats the value as currency for the current culture (lesson 18).

Asking until the input is valid

A loop can keep asking until it gets something usable. (Loops are explained in lessons 14–15.)

C#
int age;
while (true)
{
    Console.Write("Age: ");
    string? input = Console.ReadLine();
    if (input is null)
    {
        Console.WriteLine("No input.");
        return;
    }
    if (int.TryParse(input, out age) && age is >= 0 and <= 130)
    {
        break;
    }
    Console.WriteLine("That's not a valid age, try again.");
}
Console.WriteLine($"In ten years you'll be {age + 10}.");
Input
twenty
-4
29
Output
Age: That's not a valid age, try again.
Age: That's not a valid age, try again.
Age: In ten years you'll be 39.

Checking for null stops the program looping forever if input runs out. The is >= 0 and <= 130 part is a pattern (lesson 44) that checks a range in one go.

Several values on one line

Split breaks a line into pieces. StringSplitOptions.RemoveEmptyEntries ignores extra spaces between them:

C#
Console.Write("Enter numbers separated by spaces: ");
string line = Console.ReadLine() ?? "";
string[] parts = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);

int sum = 0;
foreach (string part in parts)
{
    sum += int.Parse(part);
}
Console.WriteLine($"{parts.Length} numbers, sum {sum}");
Input
4  15 8   23
Output
Enter numbers separated by spaces: 4 numbers, sum 50

Reading single keys

Console.ReadKey() returns as soon as one key is pressed, without waiting for Enter — useful for menus and "press any key" prompts. It needs a real interactive terminal, so it fails when input is redirected:

C#
Console.Write("Continue? (y/n) ");
ConsoleKeyInfo key = Console.ReadKey();
Console.WriteLine();
if (key.Key == ConsoleKey.Y)
{
    Console.WriteLine("Continuing...");
}

Try it yourself

Run this with dotnet run Program.cs, type a temperature in Celsius and press Enter.

Try it yourself · Program.cs
Edit the code, copy it into Program.cs and run it with dotnet run Program.cs (.NET 10 or later). How to set up .NET
Input
21.5
Output
Temperature in °C: 21.5 °C = 70.7 °F