In this article, we are going to talk about the difference between int.Parse() and Convert.Toint32().

Both the int.Parse() and the Convert.ToInt32() methods are used to convert a string to an integer. Although the two methods are used for the same purpose, there are, of course, some differences between them. In this article, we will discuss those differences.

To download the source code for this article, you can visit our GitHub repository.

Let’s dive in.

What is int.Parse()?

The int.Parse() method converts a given string representation of a number to its equivalent integer:

var inputString = " 123 ";
var outputInteger = int.Parse(inputString);

We pass the inputString parameter in the int.Parse() method. It returns the equivalent integer to the variable outputInteger. The int.Parse() method has an overload that can take other parameter types. We can set the specific style and culture-specific format as well.

This discussion is beyond the scope of this article so we’ll focus only on the int.Parse(string s) method.

What is Convert.Toint32()?

Convert.ToInt32() is also a method that converts a string into its corresponding integer just like the int.Parse() method:

var inputString = " 123 ";
var outputInteger = Convert.ToInt32(inputString);

We pass the same string inputString to the Convert.ToInt32() method. It sets the desired integer to the variable outputInteger.

Difference Between int.Parse() and Convert.ToInt32()

We can see the difference if we pass a null value to both methods:

string inputNullString = null;

try
{
    var outputInteger = int.Parse(inputNullString);
    Console.WriteLine(outputInteger);
}
catch (ArgumentNullException ex)
{
    Console.WriteLine("The int.Parse() method throws ArgumentNullException.");
}

var outputIntegerWithConvert = Convert.ToInt32(inputNullString);
Console.WriteLine($"The value of the outputIntegerWithConvert variable is: {outputIntegerWithConvert}");

Here, we pass a null value and see the difference as expected:

The int.Parse() method throws ArgumentNullException.
The value of the outputIntegerWithConvert variable is: 0

The int.Parse() method has failed to parse the string. So, it has thrown an ArgumentNullException. But the Convert.ToInt32() method hasn’t thrown an exception but returned 0. So, if we use the Convert.ToInt32() method, we won’t have to worry about nulls.

This is an advantage of using the Convert.ToInt32() method over the int.Parse() method.

When Should We Use int.Parse() vs Convert.ToInt32()?

If we know that we’ll always get an integer as a string form, we can use the int.Parse() method. But if there is a chance that the string can be null or an object or other type, we should use the Convert.ToInt32() method.

While we’re talking about these methods, let’s also mention a int.TryParse() method that offers an even better way to parse strings to integers in C#.

The int.TryParse() Method

The int.TryParse() method converts a string to an integer but it also returns a value as well that indicates whether the operation succeeded. So, we can pass any type of value to it.

Wanna join Code Maze Team, help us produce more awesome .NET/C# content and get paid? >> JOIN US! <<

If it succeeds to convert to an integer, it saves that integer to the output variable and returns true otherwise, it returns false:

if (int.TryParse(" 123 ", out var outputInteger))
    Console.WriteLine($"Output value is: {outputInteger}"); // Output value is: 123

We pass the " 123 " as a string and outputInteger as out parameter to the int.TryParse() method. As it is a valid integer format, the int.TryParse() method converts the string to an integer successfully and executes the if block. But if it fails to convert, the integer will default to 0.

In both cases, there was no exception and that means we can pass any string value in the int.TryParse() method. Again, if we are not sure whether the string is parsable, we should use the int.TryParse() method. Besides avoiding exceptions, it is faster than the other two methods we’ve talked about.

Conclusion

In this article, we have learned about the int.Parse() method and the Convert.toInt32() method and their differences. We also discussed the int.TryParse() method and why it is better than the previous two methods.