home / software / tips and tricks / Difference between Int32.Parse(), Convert.ToInt32(), and Int32.TryParse()

### Difference between Int32.Parse(), Convert.ToInt32(), and Int32.TryParse()

 Category: C# Updated:  10/22/2010 13:10 PM Author:  Shiju Mathews Status:    Resolved. Int32.parse(string) Int32.Parse (string NumberString) method converts the string representation of a number to its 32-bit signed integer equivalent. When NumberString is a null reference, it will throw ArgumentNullException. If NumberString is other than integer value, it will throw FormatException. When NumberString represents a number less than MinValue or greater than MaxValue, it will throw OverflowException. string NumberString1 = "555"; string NumberString2 = "555.11"; string NumberString3 = null; string NumberString4 = "23867564357846543785648756456437854356"; //Big Number int result; bool success; result = Int32.Parse(NumberString1); // Convert to 555 result = Int32.Parse(NumberString2); // Throw FormatException result = Int32.Parse(NumberString3); // Throw ArgumentNullException result = Int32.Parse(NumberString4); // Throw OverflowException Convert.ToInt32(string) Convert.ToInt32(string NumberString) method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method. When NumberString is a null reference, it will return 0 rather than throw ArgumentNullException. If NumberString is other than integer value, it will throw FormatException. When NumberString represents a number less than MinValue or greater than MaxValue, it will throw OverflowException. result = Convert.ToInt32(NumberString1); // Convert to 555 result = Convert.ToInt32(NumberString2); // ThrowFormatException result = Convert.ToInt32(NumberString3); // Convert to 0 result = Convert.ToInt32(NumberString4); // Throw OverflowException Int32.TryParse(string, out int) Int32.Parse(string, out int) method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise. This method is available in C# 2.0. When s is a null reference, it will return 0 rather than throw ArgumentNullException. If s is other than an integer value, the out variable will have 0 rather than FormatException. When NumberString represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException. For example: success = Int32.TryParse(NumberString1, out result); //success => true; result => Convert to 555 success = Int32.TryParse(NumberString2, out result); //success => false; result => Convert to 0 success = Int32.TryParse(NumberString3, out result); //success => false; result => Convert to 0 success = Int32.TryParse(NumberString4, out result); //success => false; result => Convert to 0 Convert.ToInt32 is better than Int32.Parse since it returns 0 rather than an exception. But again, according to the requirement, this can be used. TryParse will be the best since it always handles exceptions by itself.
Tags: Difference between Int32.Parse() Convert.ToInt32() and Int32.TryParse()
Updated on: October 2023