0. The Real Goal of This Lesson
The goal of this lesson is not to memorize int.Parse.
The real point is to understand this:
A computer never trusts user input.
Therefore, converting a string into a number must be an explicit, intentional step.
If this makes sense, parsing will never feel arbitrary again.
1. A Common Assumption We Were Making
Most beginners unconsciously assume this:
“If the user types a number,
then the program receives a number.”
This assumption is completely wrong.
2. The True Nature of Console.ReadLine()
var input = Console.ReadLine();
One fact must never be forgotten:
Console.ReadLine()always returns astring. No exceptions.
Why is it designed this way?
From the computer’s perspective, the user might type:
123-5ABC12.5- nothing at all (just Enter)
At compile time, this cannot be predicted.
So C# makes a deliberate design choice:
“Do not guess.
Treat everything as a string.”
3. Why This Code Does Not Compile
int number = Console.ReadLine(); // Compile-time error
From the compiler’s point of view:
-
Console.ReadLine()returnsstring -
numberexpectsint
This violates the type contract.
Important takeaway:
The compiler does not care what the user might type.
It only cares about guaranteed types.
4. What We Must Do Instead
If we want to treat user input as a number, we must say so explicitly.
In other words:
“I am taking responsibility for interpreting this string as a number.”
This process is called parsing.
5. What Parsing Actually Means
Parsing is the act of interpreting a string
as another meaningful type.
Examples:
-
"123"→int123 -
"true"→booltrue -
"2026-01-01"→DateTime
Parsing is not automatic. It is intentional.
6. Parsing a String into an Integer
In C#, the most basic form is:
int number = int.Parse(input);
What happens here:
-
inputis astring -
int.Parseattempts to interpret it as an integer - If successful, a real
intis returned - If not, execution fails (covered in the next lesson)
7. Full Execution Flow (Runnable Example)
using System;
class Program
{
static void Main()
{
Console.WriteLine("Enter a number:");
var input = Console.ReadLine(); // always string
int number = int.Parse(input); // string → int
Console.WriteLine(number + 10);
}
}
Step-by-step reasoning:
- User enters
5 -
inputbecomes"5"(string) -
int.Parse("5")produces5(int) -
number + 10results in15
8. Why C# Forces This Step
This is the most important question.
C# follows this philosophy:
“Risky operations must be explicit.”
Converting a string to a number is risky because:
- The string might not represent a number
- It could be empty
- It could be malformed
So C# refuses to guess.
Instead, it requires the developer to consciously say:
“Yes, I want to interpret this as a number.”
9. The Correct Mental Model
Incorrect mindset:
- “
ReadLine()might return a number” - “The compiler should figure this out”
Correct mindset:
User input is always a string.
If I want a number, I must explicitly parse it.
10. Why the Next Lesson Matters
There is still an unresolved problem:
int.Parse("ABC"); // Runtime failure
This does not fail at compile time.
It fails during execution.
Next topics naturally follow:
TryParse- Exceptions
- How to prevent runtime crashes
One-Line Summary
Console.ReadLine()always returns a string,
and converting it into a number is a responsibility the developer must explicitly accept.
Top comments (0)