C# Convert string to int

C#  C# 

In C#, there are there ways for converting string to int. Each way has its own advantages and disadvantages. These ways are:

  1. int.Parse method
  2. int.TryParse method (Best way)
  3. Convert.ToInt32() method

int.Parse method

int.Parse method converts string input into integer. It takes the string input and returns int as output.

static void Main(string[] args)
    string strInNumbers = "98442878";

    int number = int.Parse(strInNumbers); //Success

    Console.WriteLine(number); // Print 98442878

Before using the int.parse, we must sure the string has a valid number. If we pass invalid string to int.Parse method, then it throws FormatException like shown below:

static void Main(string[] args)
    string strAlphaNumbers = "4343REK";
        int secondInteger = int.Parse(strAlphaNumbers); //throws exception
    catch (FormatException ex)

Above code will throw a format exception with the message "Input string was not in a correct format.

int.TryParse Method

int.TryParse introduced in C# to fill the shortcoming of int.Parse method. TryParse method does not throw an exception when we passed invalid data. It returns true if the argument is a valid integer, or returns false if argument is an invalid integer.

int.TryParse returns the data into an out variable which is second argument of TryParse method. int.TryParse example is shown below:

static void Main(string[] args)
    string strInNumbers = "123434";
    int output;
    if (int.TryParse(strInNumbers, out output) == true)
        Console.WriteLine(output);  //Print 123434
        Console.WriteLine("Invalid number");

In case of invalid number argument, TryParse method returns 0 in the output variable.

We can use TryParse method with if condition or without if conditions. Below are the examples of using without if conditions.

int newNumber;

int.TryParse("8978KAPIL", out newNumber);   // Invalid integer
Console.WriteLine(newNumber);  //Print 0

int.TryParse("8473", out newNumber); // Valid integer
Console.WriteLine(newNumber); // Print 8473

Convert.ToInt32 Method

Convert is a static helper class in C# to change the data type of argument to another data type. There are three methods available to convert string data type to int.

  1. Convert.ToInt16
  2. Convert.ToInt32
  3. Convert.ToInt64

Convert methods works with any data type whether it is string, long, decimal, byte, bool or any other data type. Always use ToInt32 method, if we are working with int data type.

Convert.ToInt32 method does not work with invalid data, it throws a FormatException.

This method is also very helpful when we want to convert char data to ASCII value. Below is the example of using Convert.ToInt32 method.

static void Main(string[] args)
    int i1 = Convert.ToInt32("843793");
    Console.WriteLine(i1); //Print 843793
    int i2 = Convert.ToInt32("0099fkl");    //Throws FormatException

    int i3 = Convert.ToInt32('a');  
    Console.WriteLine(i3);  // Print 97

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

Final Words

C# provides different helper methods to convert string to int. int.TryParse method is the best way to convert any string to int. TryParse returns true if argument is valid integer else returns false. It returns the output into an out variable.

Join our newsletter and get an occasional email with a technology and dotnetpattern.com news update.