How to convert string to int/integer in c#


As most of the conversion is handled by the convert class of the system, in this scenario we will use the same convert class for converting the string data type to an integer data type. Here (To int 32) is the associative method of convert class which we can use to convert the string to an integer. In following code sample you can see and observe how we can convert string data type to integer data type.

 C# |  copy code |? 
string some_integer_value="123";
        int abc = Convert.ToInt32(some_integer_value);

Beside convert class you can also use parse and tryparse difference between them is only that if the format of string is not correct for conversion then parse method will throw exception but tryparse will not throw exception it will return false. 

Leave a Comment

Your email address will not be published.