Skip to main content
๐Ÿ“œ WAYPOINT LESSON

TryParse: the no-throw parser

โญ beginnerโณ 14 min read๐Ÿ“ Lesson 12 of 85

The out-parameter pattern, validation loops, and building robust input handling.

The pattern

bool ok = int.TryParse("42", out int value);   // ok = true,  value = 42
bool bad = int.TryParse("abc", out int other); // bad = false, other = 0

TryParse attempts the conversion and reports success as a bool. The parsed value arrives through an out parameter โ€” a second return channel. On failure the out value is 0 (a well-defined default), never garbage.

The idiomatic shape combines declaration and use:

if (int.TryParse(input, out int count))
{
    Console.WriteLine($"Got {count}");
}
else
{
    Console.WriteLine("Not a whole number.");
}

Validation: refuse politely, specifically

A robust reader distinguishes why input is invalid:

static string Classify(string input)
{
    if (input.Trim().Length == 0)
        return "empty";
    if (!int.TryParse(input, out int n))
        return "not-a-number";
    if (n < 0)
        return "negative";
    return $"ok:{n}";
}

Empty first, then format, then range โ€” each failure mode gets its own message. That specificity is what separates a program people can use from one that just says "error".

Looping until valid (console shape)

int age;
while (true)
{
    Console.Write("Age: ");
    string line = Console.ReadLine() ?? "";
    if (int.TryParse(line, out age) && age >= 0 && age <= 130) break;
    Console.WriteLine("Please enter a whole number 0โ€“130.");
}

The loop contains the validation, so the rest of the program can trust age. That's the deeper principle: validate at the boundary, then write code that assumes validity.

โšก Now practice

Ready to Code
Parse and validateTurn raw strings into trustworthy numbers โ€” with failure modes that teach, not crash.
4 challenges ยท ยท ~30 min