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.

  • 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.

When we run above example, then we get below result.

Int32.TryParse OutPut 1.0

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.

When we run above example, then we get below result.

Int32.TryParse OutPut 1.1

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 ValueIs Conversion Successful ?Result Value
"12345"true12345
" 11134 "true11134
"00111"true111
"null"false0
"12345.5"false0
"1234567891011234"false0
"1234,56"false0
"1234.00"false0
"0x80C1"false0
" "false0

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#