LearnThatStack Ace your next interview
C# · question
Question 12 of 112

What is the difference between Parse() and TryParse() methods?

beginner
← All C# questions
Re-explain

Parse() returns the value and throws on invalid input, while TryParse() returns a bool for success and passes the value out through an out parameter.

Use Parse when invalid input means a bug, such as data your own code produced. Use TryParse for input you do not control, like user input or file contents. It avoids the cost of throwing and catching an exception for a failure you expect.

int.Parse throws a FormatException for text that is not a number, and an OverflowException when the number is out of range. On failure, TryParse sets the out value to its default, which is 0 for int. Most built-in value types have both methods, including DateTime and Guid.

// Parse - throws exception on failure
try
{
    int value1 = int.Parse("123");
    int value2 = int.Parse("abc"); // Throws FormatException
}
catch (FormatException ex) { }
// TryParse - safe conversion
if (int.TryParse("123", out int value3))
{
    // Conversion successful
}
else
{
    // Conversion failed, value3 is 0
}
Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • How do you parse "1,234.56" so a server in Germany and one in the US get the same number? Pass CultureInfo.InvariantCulture or a fixed culture, plus NumberStyles. The default overloads use the current culture, so results vary by machine.
  • How does Convert.ToInt32(string) differ from int.Parse, and what does each do with null? Convert.ToInt32 returns 0 for a null string. int.Parse throws ArgumentNullException. For other bad text both throw FormatException.
  • TryParse returns its value through out. How does out differ from ref and in? The method must assign an out argument before it returns. The caller must initialize a ref argument. in passes by reference, read-only.
  • How would you write a TryParse-style method for your own type, say an email address? Return bool, give the value through out, and don't throw for expected bad input. A strong answer adds [NotNullWhen(true)] on the out parameter.
  • Why not just wrap int.Parse in try-catch when reading user input? Throwing captures a stack trace and unwinds the stack, which is slow when failures are common. It also treats an expected outcome as an error.

What you can say

  1. Both turn a string into a value, but Parse throws on bad input, while TryParse returns a bool and gives you the value through an out parameter.
  2. So int.Parse on "abc" throws a FormatException, and on a number too big for an int it throws an OverflowException.
  3. TryParse doesn't throw for bad text. It returns false and sets the out value to its default, which is 0 for an int.
  4. Most built-in value types have both methods, DateTime and Guid included.
  5. I use TryParse for input I don't control, like a form field or a line in a file, because failure there is expected and throwing an exception for it is wasted work.
  6. I use Parse when bad input would mean a bug, like a value my own code produced, because then I want it to fail loudly, not carry on with a 0.

Weak answers to avoid

  • Says TryParse is always the better choice Swallowing a parse failure on data your own code wrote hides a bug. Say Parse fits trusted input, where a failure should be loud.
  • Ignores the bool and uses the out value On failure the out value is 0, which looks like a real number from the input. Always branch on the returned bool.
  • Names only FormatException for Parse int.Parse also throws OverflowException for a number outside the int range. TryParse returns false in both cases.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the C# cheatsheet.

← Back to all C# questions
Pro · $10/mo

98 of 112 C# answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime