C# – Convert a string to an int

There are three ways to convert a string to an int:

  • Use int.Parse(). Returns an int -or- throws an exception if the string can’t be converted to an int.
  • Use int.TryParse(). Returns false if the string can’t be converted to an int.
  • Use Convert.ToInt32(). Returns 0 if the string is null, otherwise uses int.Parse() to convert the string.

I’ll show examples of using these approaches.

Use int.Parse()

int.Parse() takes a string and converts it to a 32-bit integer. Here’s an example of using int.Parse():

string input = "123";
int number = int.Parse(input);
Code language: C# (cs)

When conversion fails

int.Parse() throws an exception if it can’t convert the string to an int. There are multiple reasons why it can fail. Here’s a few examples of attempting to convert invalid input and the resulting exceptions:

//System.FormatException: Input string was not in a correct format.
int.Parse("abc");

//System.OverflowException: Value was either too large or too small for an Int32.
int.Parse("999999999999999999999999999999999999999");

//System.ArgumentNullException: Value cannot be null.
int.Parse(null);
Code language: C# (cs)

You can catch these exceptions if you want to know why conversion failed. Otherwise, if you just want to know if it failed or not, use int.TryParse() instead.

Use int.TryParse()

int.TryParse() attempts to convert a string to an int. When conversion fails, it returns false instead of throwing an exception. Otherwise it returns true and sets the out int parameter to the parsed value.

Here’s an example of using int.TryParse():

string input = "123";

if (int.TryParse(input, out int number))
{
    //use number
    Console.WriteLine($"Input + 1: {number + 1}");
}
else
{
    Console.WriteLine($"Couldn't convert {input} to an int");
}
Code language: C# (cs)

It’s able to convert “123”, so this outputs:

Input + 1: 124Code language: plaintext (plaintext)

The int is 0 if conversion fails

When conversion fails, the int is left as 0. Here’s an example:

int.TryParse("abc", out int number);
Console.WriteLine(number);
Code language: C# (cs)

This outputs 0 because it couldn’t convert “abc” to an integer, so it leaves it as the int’s default value of 0. In most cases, you won’t want to use the int if conversion failed.

Use Convert.ToInt32()

Convert.ToInt32() has three possible outcomes:

  • Returns 0 if the string is null.
  • Throws an exception if conversion fails.
  • Returns the int.

This is really only useful if you know you’re dealing with valid input and nulls and don’t want to use int.TryParse() for some reason.

Here’s a few examples of using Convert.ToInt32():

//123
int number = Convert.ToInt32("123");

//0
int number2 = Convert.ToInt32(null);
Console.WriteLine(number2);

//System.FormatException: Input string was not in a correct format
Convert.ToInt32("a");
Code language: C# (cs)

If the string is formatted

If you’re trying to convert a formatted string with int.Parse() / int.TryParse(), use the NumberStyles parameter. Otherwise conversion will fail. Here’s an example of converting a formatted string with int.Parse():

using System.Globalization;

int negativeOneK = int.Parse(" -$1,000 ", NumberStyles.Currency);

Console.WriteLine($"Parsed int: {negativeOneK}");
Code language: C# (cs)

This outputs:

Parsed int: -1000Code language: plaintext (plaintext)

By default, this uses the current culture’s (CultureInfo.CurrentCulture) formatting.