This post will discuss how to convert a string to equivalent integer representation in C#.

1. Using Int32.Parse() method

To convert the string representation of a number to its 32-bit signed integer equivalent, use the Int32.Parse() method.

Download  Run Code

 
The Int32.Parse() method throws a FormatException if the string is not numeric. We can handle this with a try-catch block.

Download  Run Code

2. Using Int32.TryParse() method

A better alternative is to call the Int32.TryParse() method. It does not throw an exception if the conversion fails. If the conversion is failed, this method simply returns false.

Download  Run Code

3. Using Convert.ToInt32() method

The Convert.ToInt32 method can be used to convert a specified value to a 32-bit signed integer.

Download Code

 
This method throws a FormatException if the string is not numeric. This can be handled using a try-catch block.

Download  Run Code

That’s all about converting a string to an integer in C#.