int.Parse() vs Convert.ToInt32() vs int.TryParse()

This is one of the frequently asked C# interview question for the beginners. In this article I will explain differences among int.Parse(), Convert.ToInt32() and int.TryParse(). I will also describe that in which case which one should be used.


int.Parse(string) or Int32.Parse(string)

It is used to convert the string representation of a number to its 32-bit signed integer equivalent.

string strVal = "98234";
int value = int.Parse(strVal); // 98234

This method throws three different type of exceptions on the basis of data provided in argument.

ArgumentNullException: If string parameter’s value is null.

string strVal = null;
int value = int.Parse(strVal); // ArgumentNullException

FormatException: If string parameter’s value is not an integer or not in proper format.

string strVal = "1098.88";
int value = int.Parse(strVal);  // FormatException

OverflowException: If string parameter’s value is out of integer range (less than MinValue or greater than MaxValue)

string strVal = "23949283529835923579283592823";
int value = int.Parse(strVal);  // OverflowException
string strVal = "-23949283529835923579283592823";
int value = int.Parse(strVal);  // OverflowException

Convert.ToInt32(string)

It is used to convert the string representation of a number to its 32-bit signed integer equivalent.

string strVal = "98234";
int value = Convert.ToInt32(strVal); // 98234

This method throws two different type of exceptions on the basis of data provided in argument.

If string parameter’s value is null, it returns 0 rather than throwing ArgumentNullException.

string strVal = null;
int value = Convert.ToInt32(strVal); // 0

FormatException: If string parameter’s value is not an integer or not in proper format.

string strVal = "1098.88";
int value = Convert.ToInt32(strVal);  // FormatException

OverflowException: If string parameter’s value is out of integer range (less than MinValue or greater than MaxValue)

string strVal = "23949283529835923579283592823";
int value = Convert.ToInt32(strVal);  // OverflowException
string strVal = "-23949283529835923579283592823";
int value = Convert.ToInt32(strVal);  // OverflowException

int.TryParse(string, out int) or Int32.TryParse(string, out int)

It is used to convert the string representation of a number to its 32-bit signed integer equivalent to out variable. It returns true if it is parsed successfully otherwise it returns false.

string strVal = "98234";
int value;
bool success = int.TryParse(strVal, out value); // 98234

This method does not throw any exceptions on the basis of data provided in argument.

If string parameter’s value is null, it returns 0 in out parameter rather than throwing ArgumentNullException.

string strVal = null;
int value;
bool success = int.TryParse(strVal, out value); // success = false; result = 0

If string parameter’s value is not an integer or not in proper format, it returns 0 in out parameter rather than throwing FormatException.

string strVal = "1098.88";
int value;
bool success = int.TryParse(strVal);  // success = false; result = 0

If string parameter’s value is out of integer range (less than MinValue or greater than MaxValue), it returns 0 in out parameter rather than throwing OverflowException.

string strVal = "23949283529835923579283592823";
int value;
bool success = int.TryParse(strVal);  // success = false; result = 0
string strVal = "-23949283529835923579283592823";
int value;
bool success = int.TryParse(strVal);  // success = false; result = 0

Difference

int.Parse(string val) Convert.ToInt32(string val) int.TryParse(string val, out int value)
If val is proper integerConverts string value to integer value and returns itConverts string value to integer value and returns itConverts string value to integer value, returns it in out parameter and true as return value of function
If val is nullArgumentNullExceptionReturns 0Returns 0 in out parameter and false as return value of function
If val is not an integerFormatExceptionFormatExceptionReturns 0 in out parameter and false as return value of function
If val is out of range of integerOverflowExceptionOverflowExceptionReturns 0 in out parameter and false as return value of function

Difference from Performance Point of View

Let’s go through actual code written in .net dll for these 3 functions:

int.Parse(string) or Int32.Parse(string)
public static int Parse(String s) {
   return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}

Convert.ToInt32(string)
public static int ToInt32(String value) {
   if (value == null)
        return 0;
   return Int32.Parse(value, CultureInfo.CurrentCulture);
}

int.TryParse(string, out int) or Int32.TryParse(string, out int)
public static bool TryParse(String s, out Int32 result) {
   return Number.TryParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo, out result);
}

Here you can see that only real difference between int.Parse(string) and Convert.ToInt32() is that Convert.ToInt32() checks only for a null string before it calls down to int.Parse(string) function which is calling Number.ParseInt32.
int.TryParse(string, out int) has the same basic structure as int.Parse(string), except it calls Number.TryParseInt32 instead of Number.ParseInt32.

But all these three functions end up at the same System.Number.ParseNumber where they all spend about the same amount of time if we are passing good data(proper number in string format) because convert spends a little less but I do not think this is enough to be statistically significant.

If you will pass bad data and parsing large number of strings, TryParse will be fastest among all of them. Convert was faster than Parse as Convert handles the null string in the bad data set without throwing an exception.