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:
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):
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()returnsnullwhen 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 isstring?, 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:
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:
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:
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:
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?
- int
- char
- string (possibly null)
- object
What happens with int.Parse("abc")?
- Returns 0
- Returns -1
- Throws a FormatException
- Returns null