Int32.TryParse method converts a string value to a corresponding 32-bit signed integer value. It returns boolean value true , if conversion successful and false , if conversion failed.
Syntax & Format
All numeric primitive data types (int, decimal, float, long , bool etc) have a static method as TryParse , that can be accessed using dot (.) operator. it follows below format.
1 |
public static bool TryParse (string str, out int res); |
- string str (Input parameter) : A string input value to convert.
- int res (Output parameter) : Resultant converted value or 0 , if it fails conversion.
- true/false (Return value) : If str converted successfully, then it returns true else false.
Example 1 : A successful conversion using TryParse
Lets look at below example, here we are converting string values to their corresponding int value.
- In the Main method , we have declared 4 string variables and then we are converting those values using Int32.TryParse method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System; namespace TryParseDemo { class Program { static void Main(string[] args) { string strInput1 = "1234"; string strInput2 = "0111"; string strInput3 = " 2345"; string strInput4 = " 1890 "; int parseNumber1; int parseNumber2; int parseNumber3; int parseNumber4; bool result1 = Int32.TryParse(strInput1, out parseNumber1); bool result2 = Int32.TryParse(strInput2, out parseNumber2); bool result3 = Int32.TryParse(strInput3, out parseNumber3); bool result4 = Int32.TryParse(strInput4, out parseNumber4); Console.WriteLine("Converted values"); Console.WriteLine("*****************"); if (result1 == true) Console.WriteLine("Parsed Value of {0}: {1}", strInput1, parseNumber1); if (result2 == true) Console.WriteLine("Parsed Value of {0}: {1}", strInput2, parseNumber2); if (result3 == true) Console.WriteLine("Parsed Value of {0}: {1}", strInput3, parseNumber3); if (result4 == true) Console.WriteLine("Parsed Value of {0}: {1}", strInput4, parseNumber4); Console.Read(); } } } |
When we run above example, then we get below result.
If you look at above out put console window, here we have successfully converted string values to their integer representation using Int32.TryParse method.
Example – 2 : Failed conversion using Int32.TryParse
Lets look at below example, Here we have some invalid string values and trying to convert them.
- In the Main method , we have declared 5 string variables and then we are converting those values using Int32.TryParse method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
using System; namespace TryParseAndParse { class Program { static void Main(string[] args) { string strInput1 = "1234.5"; string strInput2 = " "; string strInput3 = "null"; string strInput4 = " 18901111111111111111111111111111 "; int parseNumber1; int parseNumber2; int parseNumber3; int parseNumber4; bool result1 = Int32.TryParse(strInput1, out parseNumber1); bool result2 = Int32.TryParse(strInput2, out parseNumber2); bool result3 = Int32.TryParse(strInput3, out parseNumber3); bool result4 = Int32.TryParse(strInput4, out parseNumber4); Console.WriteLine("Converted values using Int32.TryParse method"); Console.WriteLine("**********************************************"); if (result1 == true) Console.WriteLine("Converted Value of {0}: Output Value : {1}", strInput1, parseNumber1); else Console.WriteLine("Conversion Failed for {0} : Output Value : {1}", strInput1, parseNumber1); if (result2 == true) Console.WriteLine("Converted Value of {0} : Output Value : {1}", strInput2, parseNumber2); else Console.WriteLine("Conversion Failed for {0} : Output Value : {1}", strInput2, parseNumber2); if (result3 == true) Console.WriteLine("Converted Value of {0} : Output Value : {1}", strInput3, parseNumber3); else Console.WriteLine("Conversion Failed for {0} : Output Value : {1}", strInput3, parseNumber3); if (result4 == true) Console.WriteLine("Converted Value of {0} : Output Value : {1}", strInput4, parseNumber4); else Console.WriteLine("Conversion Failed for {0} : Output Value : {1}", strInput4, parseNumber4); Console.Read(); } } } |
When we run above example, then we get below result.
If you look at above out put console window, here in all cases the conversion failed and we are getting out put value as zero.
- string strInput1 = "1234.5"; the string variable has a decimal separator and that’s why the conversion fails, the string must contain integral digits only.
- string strInput2 = " "; In this case, the string variable has empty value, hence conversion fails.
- string strInput3 = "null"; In this case, string variable has “null” value, hence conversion fails, we cannot convert a null to its integer representation.
- string strInput4 = " 18901111111111111111111111111111 "; In this case, the string value exceeds the max value of System.Int32 ( 2147483647) , that’s why conversion fails.
One thing to note that, in all above cases even though the conversion failed but the C# compiler didn’t raise any run time exception instead returned zero.
Along with above examples, the string conversion using TryParse method will fail , if string variable has any hexadecimal digits or group separators.
Data conversion sample table using Int32.TryParse
Below table lists out some of sample input string value and their resultant output value , if converted using Int32.TryParse method.
String Value | Is Conversion Successful ? | Result Value |
---|---|---|
"12345" | true | 12345 |
" 11134 " | true | 11134 |
"00111" | true | 111 |
"null" | false | 0 |
"12345.5" | false | 0 |
"1234567891011234" | false | 0 |
"1234,56" | false | 0 |
"1234.00" | false | 0 |
"0x80C1" | false | 0 |
" " | false | 0 |
Key Notes :
Int32.TryParse method converts a string value to a corresponding 32-bit signed integer value data type. It returns a boolean value true , if conversion successful and false , if conversion failed. In case of failed conversion, it doesn’t throw any exception and zero is assigned to the out variable.
Thanks for visiting 🙂 Please check my article on TryParse() vs Parse() in C#
Thanks a lot for this.
This post helped me understand int.TryParse