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.

The standard format of Int32.TryParse method is as follows:

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

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.

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

Int32.TryParse_1

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

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()

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

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

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.

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

Int32.TryParse OutPut 1.1

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.

[table id=2 /]

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#