DEV Community

TinoMuchenje
TinoMuchenje

Posted on

Parse vs TryParse in C#

We will explore the difference between the Parse and TryParse methods in C#, and when to use each one.

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

What are Parse and TryParse methods?

The Parse and TryParse methods are used to convert a string representation of a number to a numeric type, such as int, long, double, decimal, etc. For example, if we have a string "123", we can use int.Parse or int.TryParse to convert it to an integer value 123.

The Parse and TryParse methods are defined on each numeric type, such as System.Int32, System.Double, System.Decimal, etc. They have different overloads that accept different parameters, such as the number format, the culture information, and the style of the number.

What is the difference between Parse and TryParse methods?
The main difference between Parse and TryParse methods is how they handle invalid input. If the string cannot be converted to a number, the** Parse** method will throw an exception, while the TryParse method will return false and not throw an exception.

For example, parse the string "abc" as an integer, the int.Parse method will throw a FormatException, while the int TryParse method will return false and assign a default value of 0 to the output parameter.

`
string input = "abc";
int result;

// Using Parse method
try
{
result = int.Parse(input); // This will throw an exception
Console.WriteLine(result);
}
catch (FormatException e)
{
Console.WriteLine(e.Message); // This will print "Input string was not in a correct format."
}

// Using TryParse method
if (int.TryParse(input, out result)) // This will return false
{
Console.WriteLine(result); // This will not be executed
}
else
{
Console.WriteLine("String could not be parsed."); // This will print "String could not be parsed."
}
`
The TryParse method does not use try/catch internally. It is implemented without exceptions so that it is faster and more efficient. In fact, it is likely that the Parse method calls the TryParse method internally and then throws an exception if it returns false.

When to use Parse and TryParse methods?
The general rule is to use Parse method if you are sure that the string will be valid and can be converted to a number. Otherwise, use TryParse method to avoid exceptions and handle invalid input gracefully.

For example, if you are reading user input from a console or a text box, you should use TryParse method to validate the input and display an error message if it is not a valid number. On the other hand, if you are parsing a string that is hard-coded or comes from a reliable source, you can use Parse method and assume that it will succeed.

However, you should always use exception handling when calling Parse method to catch any FormatException that may occur due to unexpected or invalid input. You can also use String.IsNullOrEmpty method to check for null or empty strings before attempting to parse them.

Top comments (0)