TryParse()

noun

say it: “TRY-pars” /ˈtraɪpɑːrs/

A method such as int.TryParse that tries to turn text into a number and says whether it worked, instead of failing.

What is TryParse() in C#?

TryParse() — A method such as int.TryParse that tries to turn text into a number. It returns true and gives you the number through out, or returns false when the text isn't a number, so your program can ask again instead of crashing.

if (int.TryParse("12", out int quantity))
{
    Console.WriteLine(quantity * 2); // 24
}

Console.WriteLine(int.TryParse("a dozen", out _)); // False

In a sentence

  • “I used int.TryParse on the user's answer, so typing a word just asked them again.”

Level: Apprentice · More C# terms at this level

Updated