Different ways to convert String to Integer in C#

Like other programming languages, in C# we can convert string to int. There are three ways to convert it and they are as follows:

  • Using the Parse Method
  • Using the TryParse Method
  • Using the Convert Method from (System.Convert class)

The input string can be anything like “10”, “10.10”, “10GeeksforGeeks”, “” (Your string can be number, a combination of characters or an Empty String).
When a given string is number or floating-point number then we can use directly any of above-listed methods to convert it from string to int but the combination of characters and an empty string will throw an error which needs to catch using exceptional handling.

1. Using the Parse Method

Here, we are calculating the area of a circle but the given input length is in string format and hence used Int32.Parse() method to convert the length from string to int (32-bit signed integer equivalent).



filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert string to 
// integer using Parse Method
using System;
  
namespace GeeksforGeeks {
  
class GFG{
  
    // Main Method
    public static void Main(string[] args)
    {
  
        // Taking a string
        string l = "10";
  
        // using the Method
        int length = Int32.Parse(l);
  
        // Finding the area of a square
        int aofs = length * length; 
  
        Console.WriteLine("Area of a circle is: {0}", aofs);
    }
}
}

chevron_right


Output:

Area of a circle is: 100

When we have a value greater than Integer: If you assign the value big value to string then it will through an OverflowException as int data type can’t handle the large values (It very much depends upon the range of data types).

string l="10000000000";

The output will be System.OverflowException: Value was either too large or too small for an Int32.

When we have Empty String: If you keep string blank then it will through an exception System Format Exception as the input is blank.

string l="";

The output will be: System.FormatException: Input string was not in a correct format.

2. Using the TryParse Method

Here, we have used TryParse() method, the difference in Parse() and TryParse() method is only that TryParse() method always return the value it will never throw an exception. If you closely look at the value of an input, then it clearly shows that it will throw an exception but TryParse() will never throw an exception. Hence the output is Zero.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert string to 
// integer using TryParse Method
using System;
  
namespace GeeksforGeeks {
  
class GFG{
  
    // Main Method
    public static void Main(string[] args)
    {
  
        // Taking a string
        string l = "10000000000";
        int length = 0;
  
        // using the method
        Int32.TryParse(l, out length);
  
        // Finding the area of a square
        int aofs = length * length; 
  
        Console.WriteLine("Area of a circle is: {0}", aofs);
    }
}
}

chevron_right


Output:

Area of a circle is: 0

3. Using the Convert Method

Here, we have used Convert.ToInt32() method, the difference in Parse() and Convert.ToInt32() method is only that Convert.ToInt32() method always accept the null value return it. Hence the output is Zero. We have used exceptional handling in this example so, try block will throw the exception if occurred and catch block will accept the exception and write whatever exception occurred.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to convert string to 
// integer using Convert Method
using System;
  
namespace GeeksforGeeks {
  
class GFG {
  
    // Main Method
    public static void Main(string[] args)
    {
  
        // Taking a string
        string l = null;
        try {
               int length = Convert.ToInt32(l);
  
               // Finding the area of a square
               int aofs = length * length; 
  
               Console.WriteLine("Area of a circle is: {0}", aofs);
        }
        catch (Exception e) {
            Console.WriteLine("Unable to convert:Exception:" + e.Message);
        }
    }
}
}

chevron_right


Output:

Area of a circle is: 0


My Personal Notes arrow_drop_up

Shubham Darda is the founder and architect of Codingwindow He is an engineer and loves the combination of ideas and the execution of an idea

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.