11. C# (Parsing)

Published: (February 9, 2026 at 03:07 PM EST)
3 min read
Source: Dev.to

Source: Dev.to

The Real Goal of This Lesson

The goal of this lesson is not to memorize int.Parse; the real point is to understand that a computer never trusts user input. Parsing is an explicit, intentional step that takes responsibility for interpreting a string as a specific type.

  • Console.ReadLine() always returns a string (never throws an exception).
  • From the computer’s perspective the user might type:
    • 123
    • -5
    • ABC
    • 12.5
    • nothing at all (just Enter)

Because the compiler cannot predict what the user will enter, C# makes a deliberate design choice: do not guess.

int number = Console.ReadLine(); // Compile-time error

Console.ReadLine() returns a string, while number expects an int; this violates the type contract. The compiler cares only about types, not about possible runtime values. If we want to treat user input as a number, we must explicitly say so—this is called parsing.

What is parsing?

Parsing is the act of interpreting a string as another type:

  • "123"int 123
  • "true"bool true
  • "2026-01-01"DateTime

Parsing is not automatic; it must be invoked intentionally.

Basic 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

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

If int.Parse cannot interpret the string (e.g., int.Parse("ABC")), execution fails at runtime (covered in later lessons).


Why C# Forces This Step

C# follows the philosophy: “Risky operations must be explicit.” Converting a string to a number is risky because the string might:

  • Not represent a number.
  • Be empty.
  • Be malformed.

Therefore, C# refuses to guess and requires the developer to consciously state the intention:

int number = int.Parse(input); // explicit conversion

Common misconceptions

  • Incorrect mindset:ReadLine() might return a number; the compiler should figure it out.”
  • Correct mindset: “User input is always a string. If I need a number, I must explicitly parse it.”

Unresolved problem

int.Parse("ABC"); // Runtime failure

This error occurs at runtime, not compile time. Subsequent topics will cover how to handle such cases safely:

  • TryParse
  • Exceptions
  • Preventing runtime crashes

Remember: Console.ReadLine() always returns a string.

0 views
Back to Blog

Related posts

Read more »

Using C# Record types with MongoDB

markdown !MongoDB Guestshttps://media2.dev.to/dynamic/image/width=50,height=50,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%...

O que são generics?

Generics são uma funcionalidade introduzida no Java 5 que permite criar classes, interfaces e métodos que trabalham com diferentes tipos de dados. Eles eliminam...