Refinance now before rates go up! Get multiple rate quotes at GetMyLender.com.

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

In this article, we will discuss what are the differences between Int32.Parse(), Convert.ToInt32() and Int32.TryParse() in C# and when and which will be useful in programs.

As you are already known about int in C#. It is used to hold integer value in memory which represents a 32-bit signed integer. While coding many times you come across int, Int16, Int32, Int64. int and Int32 are likely to be same.
All these are stuct type means value type/premetive type.
int => System.Int32 and Represents a 32-bit sigened integer.
Int16 => System.Int16 and Represents a 16-bit sigened integer.
Int32 => System.Int32 and Represents a 32-bit sigened integer.
Int64 => System.Int64 and Represents a 64-bit sigened integer.


Int32.Parse(anyString)

Int32.Parse (strAny) method converts the string representation of a number to its 32-bit signed integer equivalent. When strAny is a null reference, it will throw ArgumentNullException. If strAny is other than integer value, it will throw FormatException. When strAny represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.
For example:


            int result; 
            bool success;

            // str1 any integer number within range
            string str1 = "1234"; 

            // str2 other than integer value
            string str2 = "1234.56";
            
            // str3 with null reference  
            string str3 = null; 

            // str4 a number greater than MaxValue
            string str4 = "52487234982462340923464923869328932893246"; 
        

Results for Int32.Parse(string) :


            result = Int32.Parse(str1); //-- 1234  
            result = Int32.Parse(str2); //-- FormatException  
            result = Int32.Parse(str3); //-- ArgumentNullException  
            result = Int32.Parse(str4); //-- OverflowException  
        

Convert.ToInt32(anyString)

Convert.ToInt32(strAny) method converts the specified string representation of number to its 32-bit signed integer equivalent. When strAny is a null reference, it will return 0 rather than throwArgumentNullException. If strAny is other than integer value, it will throw FormatException. When strAny represents a number less than MinValue or greater than MaxValue, it will throw OverflowException.

Results for Convert.ToInt32(string) :


            result = Int32.Parse(str1); //-- 1234  
            result = Int32.Parse(str2); //-- FormatException  
            result = Int32.Parse(str3); //-- 0  
            result = Int32.Parse(str4); //-- OverflowException  
        

Int32.TryParse(anyString, out anyInt)

Int32.Parse(anyString, out anyInt) method converts the specified string representation of number to its 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise.When anyString is a null reference, it will return 0 rather than throw ArgumentNullException. If anyString is other than an integer value, the out variable will have 0 rather than FormatException. When anyString represents a number less than MinValue or greater than MaxValue, the out variable will have 0 rather than OverflowException.

Results for Int32.TryParse(string, out int) :


            success = Int32.TryParse(str1, out result); //-- success => true; result => 1234  
            success = Int32.TryParse(str2, out result); //-- success => false; result => 0  
            success = Int32.TryParse(str3, out result); //-- success => false; result => 0  
            success = Int32.TryParse(str4, out result); //-- success => false; result => 0  
        

Conclusion :

All these used according to the requirements. Convert.ToInt32 is better than Int32.Parse since it returns 0 rather than an exception. TryParse will be the best since it always handles exceptions by itself.


No comments:

Post a Comment