Module 2 · Input, loops and methods: make programs respond · Lesson 5 of 20
Read input safely: text becomes a number only after a check
What should happen when someone types twenty instead of 20? We need to check the text before using it as a duration.
Predict the conversion before using the keyboard
Keyboard input arrives as text. The text "25" and the int value 25 are different kinds of value. The TryParse operation tries to turn suitable text into an int. Start with fixed text so you can see the two results it provides:
using System;
string text = "25";
bool parsed = int.TryParse(text, out int minutes);
Console.WriteLine($"Parsed: {parsed}");
Console.WriteLine($"Minutes: {minutes}");What are the two output lines?
Show answer
Correct answer: B
TryParse succeeds on the text 25, returns true, and writes the number 25 into minutes.
Option A: This text is a valid int representation.
Option C: Successful parsing writes the parsed number into minutes.
Option D: The returned success value and the produced number have been swapped.
Hint: TryParse answers Did it work? and, if so, What number?
Read out int minutes as a small recipe for now: create an int variable named minutes, and let TryParse write its result there. The returned bool says whether conversion succeeded. Use the number only on the success path. A later lesson on methods explains parameter passing more deeply.
See the input journey
ReadLine may return null, which takes the missing-input path. Available text is passed to int.TryParse. Failed parsing is reported without using the numeric result. Successful parsing is followed by the nonnegative rule: -5 is rejected, while 25 is accepted. A blank line is text and fails parsing.
Parsing and validation answer different questions. Parsing asks whether the text represents a value of the required type. Our application rule then asks whether that value is allowed. For example, -5 is a valid int, but a negative study duration is not allowed by this planner.
A learner enters -5. Which statement is correct?
Show answer
Correct answer: C
A minus sign is valid in an integer representation. The nonnegative rule is a separate validation check.
Option A: Negative integers are still integers.
Option B: TryParse does not silently remove the sign.
Option D: An ordinary negative input can be handled with a condition.
Build a one-question planner
Write your attempt before reading the complete program. Print Study minutes?, read one line, and handle three kinds of result: no line, an invalid whole number, or a parsed number. A parsed number needs its own negative check.
using System;
Console.WriteLine("Study minutes?");
string? text = Console.ReadLine();
if (text == null)
{
Console.WriteLine("No input received.");
}
else if (int.TryParse(text, out int minutes))
{
if (minutes < 0)
{
Console.WriteLine("Minutes cannot be negative.");
}
else
{
Console.WriteLine($"Planned {minutes} minutes.");
}
}
else
{
Console.WriteLine("Enter a valid whole number.");
}Console.ReadLine reads a line without its ending newline. Its result can be null when no more input is available. The declaration string? text acknowledges that possibility. Pressing Enter on an otherwise blank line gives empty text; that is different from no line at all. We check null before the remaining conditions to make the distinction visible.
Run the program. When it asks, type 25 and press Enter. The application output is shown below; what you type is input, not another WriteLine instruction:
Study minutes? Planned 25 minutes.
Practise the unhappy paths
Run again for each row. Each run reads just one line; loops for repeated attempts come next.
| Input | Expected final message |
|---|---|
| 0 | Planned 0 minutes. |
| -5 | Minutes cannot be negative. |
| abc | Enter a valid whole number. |
| 2.5 | Enter a valid whole number. |
| A blank line | Enter a valid whole number. |
| No line available / end of input | No input received. |
How is a blank line different from no line available?
Show answer
Correct answer: B
ReadLine distinguishes an empty line from the absence of another line. This program handles both safely.
Option A: The application checks input; it does not treat either case as a successful zero entry.
Option C: The four letters null are text, not the null value.
Option D: ReadLine can return an empty string or null.
Independent change: allow only durations from 0 to 180. Inside the successful parse branch, reject minutes above 180 with a message. Test -1, 0, 180 and 181. Your acceptance condition must include both endpoints. Of these four test inputs, accept 0 and 180. Add this rule after you can explain the existing paths.
Explain back: Why is successful parsing not enough to accept a duration? What two pieces of information does TryParse give us?
References: Microsoft — Console.ReadLine, Int32.TryParse.
Quick reference
- Console.ReadLine returns a line of text, or null when no more input is available.
- string? explicitly allows a text value or null.
- A blank line is empty text, not the null value.
- int.TryParse returns success/failure and writes the parsed number through out.
- Use the number on the successful parse path.
- Check application rules after parsing; a negative integer can be valid syntax but invalid study time.
TechInterviewNotes Editorial Team · Updated 27 Oct 2026 ·