Int32.TryParse is a static data conversion method that allows to convert a string value to a corresponding 32-bit signed integer value. It returns a Boolean True value for successful conversion and False in case of failed conversion.
Syntax & Format of TryParse method :
All numeric primitive data types (int, decimal, Double, float, long , bool etc) has a static TryParse method that is accessible using dot (.) operator as follows.
1 2 3 4 5 6 |
Int32.TryParse(string str, out Int32 res)// Int32 type Int16.TryParse(string str, out Int16 res)// Int16 type Int64.TryParse(string str, out Int64 res)// Int64 type decimal.TryParse(string str, out decimal res)// decimal type float.TryParse(string str, out float res)// float type bool.TryParse(string str, out bool res)// bool type |
The standard format of Int32.TryParse method is as follows:
1 |
public static bool TryParse (string str, out Int32 res); |
- string str (Input parameter) : A string input value to convert.
- Int32 res (Output parameter) : The resultant converted 32-bit signed integer value or 0 if it fails conversion.
- True/False (Return value) : If str converted successfully, then it returns True else False .
Conversion Using Int32.TryParse() method :
As per the format above, in below example we have an input string variable string strIn = "0111"; and a variable as Int32 intRes; to hold the parsed Int32 value.
1 2 3 4 5 |
string strIn = "0111"; Int32 intRes; if(Int32.TryParse(strIn, out intRes)) Console.WriteLine(intRes); //Prints 111 |
This statement if(Int32.TryParse(strIn, out intRes)) executes and returns True/False value based on the conversion.
In this case, it successfully converts the string strIn = "0111" to 111 and returns a boolean True , the out parameter intRes holds the parsed value 111.
Complete Example :
In below example, we are converting string values to their corresponding Int32 value.
In the Main method , we have declared 4 string variables and then trying to convert 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 |
using System; namespace TryParseSample { class Program { static void Main(string[] args) { string strInput1 = "1234"; string strInput2 = "0111"; string strInput3 = " 2345"; string strInput4 = " 1890 "; Int32 parseNumber1; Int32 parseNumber2; Int32 parseNumber3; Int32 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("Conversion Successful, Parsed Value of {0}: {1}", strInput1, parseNumber1); if (result2 == true) Console.WriteLine("Conversion Successful, Parsed Value of {0}: {1}", strInput2, parseNumber2); if (result3 == true) Console.WriteLine("Conversion Successful, Parsed Value of {0}: {1}", strInput3, parseNumber3); if (result4 == true) Console.WriteLine("Conversion Successful, 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 Int32 representation using Int32.TryParse method.
Parsing a NULL value using Int32.TryParse() method:
Lets take below example, where we trying to parse null using Int32.TryParse
1 2 3 4 5 6 7 |
string strInput = null; Int32 outRes; if (Int32.TryParse(strInput, out outRes)) Console.WriteLine(outRes); else Console.WriteLine(outRes);//Prints 0 |
In above , the runtime fails to convert null to Int32 representation, it returns Boolean False and the out variable Int32 outRes; holds 0 as output value.
Parsing a different input type using Int32.TryParse() method :
Lets take below example, here we are trying to parse a string of decimal value using Int32.TryParse()
1 2 3 4 5 6 7 |
string strInDecimal = "1234.5"; Int32 outRes; if (Int32.TryParse(strInDecimal, out outRes)) Console.WriteLine(outRes); else Console.WriteLine(outRes);//Prints 0 |
In above case string strInDecimal = "1234.5"; the variable has a decimal separator and that’s why the conversion fails, it returns Boolean False and the out variable Int32 outRes; holds 0 as output value.
Parsing a empty string value using Int32.TryParse() method:
Lets take below example, where we trying to parse empty string using Int32.TryParse
1 2 3 4 5 6 7 |
string strInput = " "; Int32 outRes; if (Int32.TryParse(strInput, out outRes)) Console.WriteLine(outRes); else Console.WriteLine(outRes);//Prints 0 |
In above , the runtime fails to convert ” ” to Int32 representation, it returns Boolean False and the out variable Int32 outRes; holds 0 as output value.
Parsing max Int32 value using Int32.TryParse() method:
Lets take below example, where we trying to parse a string value larger than max Int32 using Int32.TryParse
1 2 3 4 5 6 7 |
string strInMax =" 18901111111111111111111111111111 "; Int32 outRes; if (Int32.TryParse(strInMax, out outRes)) Console.WriteLine(outRes); else Console.WriteLine(outRes);//Prints 0 |
In this case, the string value “18901111111111111111111111111111” exceeds the max value of System.Int32 ( 2147483647) , that’s why conversion fails.
The runtime returns Boolean False and the out variable Int32 outRes; holds 0 as output value.
Complete example of failed conversion test case:
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.
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 0 is assigned to the out variable.
- The string conversion fails, if the input string parameter is null or System.String.Empty
- It also fails, if the input string represents a number less than System.Int32.MinValue or greater than System.Int32.MaxValue.
- The conversion fails, if string variable has any hexadecimal digits or group separators.
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