Site icon

Int32.TryParse Method in C#

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:

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.

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.

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 :

Thanks for visiting 🙂 Please check my article on TryParse() vs Parse() in C#

Exit mobile version