DEV Community

Sabin Sim
Sabin Sim

Posted on

11. C# (Parsing)

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();
Enter fullscreen mode Exit fullscreen mode

One fact must never be forgotten:

Console.ReadLine() always returns a string. No exceptions.


Why is it designed this way?

From the computer’s perspective, the user might type:

  • 123
  • -5
  • ABC
  • 12.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
Enter fullscreen mode Exit fullscreen mode

From the compiler’s point of view:

  • Console.ReadLine() returns string
  • number expects int

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"int 123
  • "true"bool true
  • "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);
Enter fullscreen mode Exit fullscreen mode

What happens here:

  1. input is a string
  2. int.Parse attempts to interpret it as an integer
  3. If successful, a real int is returned
  4. 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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Step-by-step reasoning:

  1. User enters 5
  2. input becomes "5" (string)
  3. int.Parse("5") produces 5 (int)
  4. number + 10 results in 15

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
Enter fullscreen mode Exit fullscreen mode

This does not fail at compile time.
It fails during execution.

Next topics naturally follow:

  1. TryParse
  2. Exceptions
  3. 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)