All about Int32.Parse, Convert.ToInt32 and Int32.TryParse

Some days back I wrote one chunk of code for fixing one of the client specific defects in one of application on which I was working. I used Int32.Parse for parsing string value to integer. After some days the issue resurfaced again. I debugged the code for finding the root cause behind the issue, and I found that for some values Int32.Parse was failing.

Then I decided to dig into all the options available for parsing and just thought to share my findings with you.

First of all let us check some string values which I am going to pass to the these converter for parsing.

string sNull = null;

string sRandomString = "abcdefghijkl";

string sCorrectString = "123";

string sOverflowException = "12345890690123456789012345997890";

string sBlank = string.Empty;

int result;

1.Int32.Parse(string s) : –
Converts the string representation of a number to it’s 32 bit signed integer equivalent.
Assembly:- mscorlib.dll.

Int32.Parse can throw three kind of exception –

  1. ArgumentNullException
  2. FormatException
  3. OverFlowException

Here are some examples with output –

 Console.WriteLine(Int32.Parse(sNull)); //ARgumentNullExcception. Value can not be NULL.

 Console.WriteLine(Int32.Parse(sRandomString)); // FormatException(Input string was not in a correct format.)

 Console.WriteLine(Int32.Parse(sCorrectString)); // Success(prints 123)

 Console.WriteLine(Int32.Parse(sOverflowException)); //OverflowException(Value is too large for an Int32)

Console.WriteLine(Int32.Parse(sBlank)); // FormatException(Input string was not in a correct format.)

2.Convert.ToInt32(string s) :-
Converts the string representation of any integer to it’s 32 bit equivalent signed integer.
Assembly : – mscorlib.dll

Let us check how Convert.ToInt32 was implemented. If you will check the reflector( ) into mscorlib, you will see the following code for Convert.ToInt32.

 public static int ToInt32(string value)
 if (value == null)
 return 0;
 return int.Parse(value, CultureInfo.CurrentCulture);

So if you notice Convert.ToInt32 is using int.Parse behind the scene. But the most important thing here is we are not going to get ArgumentNullException, because Convert.ToInt32 handles the null comparisons behind the scene.

But we will be still getting the two Exceptions –

  1. FormatException
  2. OverflowException

Let us check some examples –

Console.WriteLine(Convert.ToInt32(sNull)); // Returns 0(check the implementation in mscorlib)

 Console.WriteLine(Convert.ToInt32(sRandomString)); // FormatException was unhandeled

 Console.WriteLine(Convert.ToInt32(sCorrectString));// return correct Value 123

 Console.WriteLine(Convert.ToInt32(sOverflowException)); //OverflowException was unhandled

 Console.WriteLine(Convert.ToInt32(sBlank)); // FormatException was unhandeled

3.Int32.TryParse(string s, out result) : –
Converts the string representation of any number to it’s equivalent 32 bit signed integer. A return value indicates whether the parsing was successful or not.

Assembly: – mscorlib.dll
Return Type : – bool

Note :- In case of successful conversion it will return True otherwise false.

How to use Int32.TryParse?

Console.WriteLine(string.Format("Return Type = {0} -- result = {1}", Int32.TryParse(sNull, out result), result)); //Return Type = False -- result = 0

Console.WriteLine(string.Format("Return Type = {0} -- result = {1}", Int32.TryParse(sRandomString, out result), result));//Return Type = False -- result = 0

Console.WriteLine(string.Format("Return Type = {0} -- result = {1}", Int32.TryParse(sCorrectString, out result), result)); //Return Type = True -- result = 123

Console.WriteLine(string.Format("Return Type = {0} -- result = {1}", Int32.TryParse(sOverflowException, out result), result)); //Return Type = False -- result = 0

Console.WriteLine(string.Format("Return Type = {0} -- result = {1}", Int32.TryParse(sBlank, out result), result)); //Return Type = False -- result = 0

Let us examine the results –

If you will notice there is no exception at all. We are getting the True in case of successful parsing along with the correct result value and False in case of failed conversion with result value zero. (This is very important, we are getting result value zero in case of failed conversion).
You can easily check the code behind implementation of Int32.TryParse using ILSpy(

Comparisons : –

On the basis of Robustness –
Int32.TryParse > Convert.ToInt32 > Int32.Parse

On the basis of performance –
Int32.Parse > Convert.ToInt32 > Int32.TryParse

Please leave a reply below, If you want to add something.

Written By: Mukund Narayan, ASP.Net Developer, Mindfire Solutions


Posted on January 14, 2014, in C# and tagged , , , , , , , , , . Bookmark the permalink. 4 Comments.

  1. Ravindra Singh Mehta

    Thnx for awesome post sir ji..

  2. Mukund Narayan Jha

    Thank you Ravindra

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: