TExcelTAD
Home › C# › C# User Input

C# User Input

Console applications read what the user types with Console.ReadLine(). It waits until the user presses Enter and returns the whole line as a string (without the line break).

Note: The playgrounds on this site run without a keyboard attached, so examples that actually wait for input are shown as plain code. Runnable examples use fixed strings in place of what a user would type — the processing logic is exactly the same.

Reading a line of text

On your own computer, this program asks for a name and greets the user:

csharp
using System;

Console.Write("What is your name? ");
string name = Console.ReadLine();
Console.WriteLine(
quot;Hello, {name}!");

A session in the terminal looks like this (the user typed Maria):

text
What is your name? Maria
Hello, Maria!

Console.Write (not WriteLine) keeps the cursor on the same line as the question, which looks more natural.

Note: ReadLine() returns null when there is no more input — for example when input is redirected from an empty file. In projects with nullable reference types enabled its return type is string?, and the compiler reminds you to handle that case: string name = Console.ReadLine() ?? "";

Input is always text

ReadLine returns a string even when the user types digits. To calculate with it you must convert it, as covered in the Type Casting lesson:

csharp
Console.Write("Enter your age: ");
int age = int.Parse(Console.ReadLine());
Console.WriteLine(
quot;Next year you will be {age + 1}.");

This works for valid input, but if the user types twenty, int.Parse throws a FormatException and the program crashes. Let's simulate that processing with a fixed value:

csharp · try it yourself
using System;

string typed = "29";   // pretend the user typed this
int age = int.Parse(typed);
Console.WriteLine(
quot;Next year you will be {age + 1}.");
Next year you will be 30.

Validating input safely

Robust programs use int.TryParse and ask again until the input is valid. The loop below shows the pattern; here the "user" is an array of answers so the example can run:

csharp · try it yourself
using System;

string[] answers = { "", "ten", "-4", "12" }; // simulated keyboard input
int index = 0;
int quantity;

while (true)
{
    Console.Write("Quantity: ");
    string input = answers[index++];
    Console.WriteLine(input);

    if (int.TryParse(input, out quantity) && quantity > 0)
        break;

    Console.WriteLine("Please enter a positive whole number.");
}

Console.WriteLine(
quot;You ordered {quantity} items.");
Quantity: 
Please enter a positive whole number.
Quantity: ten
Please enter a positive whole number.
Quantity: -4
Please enter a positive whole number.
Quantity: 12
You ordered 12 items.

With real input, you would simply replace answers[index++] with Console.ReadLine().

Cleaning up input

Users add stray spaces and mix upper and lower case. Normalise input before comparing it:

csharp · try it yourself
using System;

string raw = "  YES ";                  // what the user typed
string answer = raw.Trim().ToLower();

if (answer == "yes" || answer == "y")
    Console.WriteLine("Confirmed.");
else
    Console.WriteLine("Cancelled.");
Confirmed.

Other Console input methods

Method Returns Use
Console.ReadLine() string? Read a full line of text
Console.ReadKey() ConsoleKeyInfo Read a single key press (e.g. "press any key")
Console.Read() int Read the next character code, or -1 at end of input

Test yourself

What type does Console.ReadLine() return?

  1. int
  2. char
  3. string (possibly null)
  4. object

What happens with int.Parse("abc")?

  1. Returns 0
  2. Returns -1
  3. Throws a FormatException
  4. Returns null