Tag Archives: ArgumentNullException

Difference between Int32.Parse(), Int32.TryParse() & Convert.ToInt32()

Some days back I stuck into a problem where Convert.ToInt32 was giving an exception some times. That day for the first time I explored the differences between the 3 Casting methods:

  • Convert.ToInt32()
  • Int32.Parse()
  • Int32.TryParse()

So today I am going to share with you all the difference between Int32.Parse(string), Int32.TryParse(string) and Convert.ToInt32(string) and will discuss when to use which one.

All the three casting method converts the string representation of a number to its 32-bit signed integer equivalent. Int32.parse(string) and Convert.ToInt32(string) will either return value or throw exception in case if conversion fails.  In fact Convert.ToInt32 internally calls Int32.Parse for conversion. On the other hand Int32.TryParse(string, out int) will return true if conversion is success and false if not. And the converted value will be stored in the 2nd parameter.

For clear understanding we will use the following data as an example

string s1 = null;

string s2 = “abcdefghijkl”;

string s3 = “123”;

string s4 = “123456789012345678901234567890”;

string s5 = “”;

int result;

bool sucess;

Now, lets take up these methods one by one:

Int32.Parse(string s)

In Int32.Parse(string s), if s is a null reference, it will throw ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

result = Int32.Parse(s1);     // ArgumentNullException
result = Int32.Parse(s2);     // FormatException
result = Int32.Parse(s3);     // 123
result = Int32.Parse(s4);     // OverflowException
result = Int32.Parse(s5);     // FormatException

Convert.ToInt32(string s)

Convert.ToInt32(string s) calls in turn Int32.Parse(string s) method. When s is a null reference, it will return 0 rather than throwing ArgumentNullException. If s is other than integer value, it will throw FormatException. When s represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

result = Convert.ToInt32(s1);     // 0
result = Convert.ToInt32(s2);     // FormatException
result = Convert.ToInt32(s3);     // 123
result = Convert.ToInt32(s4);     // OverflowException
result = Convert.ToInt32(s5);     // FormatException

Int32.TryParse(string s)

Int32.TryParse(string s) is available in C# 2.0 and above. When s is a null reference, it will return 0 rather than throwing  ArgumentNullException. If s is other than an integer value, the out variable will have 0 rather than FormatException. When s represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException.

success = Int32.TryParse(s1, out result);     // success = false; result = 0
success = Int32.TryParse(s2, out result);     // success = false; result = 0
success = Int32.TryParse(s3, out result);     // success = true; result = 123
success = Int32.TryParse(s4, out result);     // success = false; result = 0
success = Int32.TryParse(s5, out result);     // success = false; result = 0

So now the question is which one should be used:

With respect to the Performance(Speed) order is

Int32.Parse(string) > Convert.Int32(string) > Int32.TryParse(string, out int)

Int32.Parse() method is the fastest and the most time consuming method is Int32.TryParse() because it handles all of type exceptions and never ever throw any exception.

and

With respect to the Robustness of the methods preference order is:

Int32.TryParse(string, out int) > Convert.ToInt32(string) > Int32.Parse(string)

Int32.Parse never checks for any kind of exception if the input is integer type it will provde the output and in all other cases it will throw an exception.

Convert.ToInt32 is better then Int32.parse because it at least check for null reference and returns 0 in case of null input.

Int32.TryParse is the most robust method. It handle all the case and make sure no exception is thrown, whatever the case maybe.

Finally, it depends on your requirements, you need to decide which one is best for you. Whether you require fastest method or the safest one.

Happy Coding…

Tagged , , , , , , , , , , , , , ,