Monday, October 17, 2022

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

 => int.Parse() will throw an exception

=> int.TryParse() will return false (but not throw an exception)


Parse throws an exception if the conversion from a string to the specified datatype fails, whereas TryParse explicitly avoids throwing an exception.

Example:

int number = int.Parse(textBoxNumber.Text);

// The Try-Parse Method

int.TryParse(textBoxNumber.Text, out number);

No comments:

Post a Comment