How to Convert String Representation Number To Integer In C#

In this article will see how to convert string to integer using the following functions,

  • Parse()
  • Convert
  • TryParse()

Parse() method

This Parse method is present in all wrapper classes like Int16, Int32, Float, Double, etc. Commonly parse method used to convert values from one datatype to another datatype. For numeric values there are Parse methods available for 16, 32, 64 bit signed integer types.

Int16.Parse() //result in short
Int32.Parse() //result in integer
Int64.Parse() //result in long datatype
class StringToNumber {
    public static void Main() {
        try {
            //parsing different values
            string inputShort = "904";
            short valueShort = Int16.Parse(inputShort);
            Console.WriteLine(" '{0}' Int16.Parse short- {1}", inputShort, valueShort);
            string inputInt = "12345";
            int valueInt = Int32.Parse(inputInt);
            Console.WriteLine("'{0}' Int32.Parse int- {1}", inputInt, valueInt);
            string inputLong = "21345678904";
            long valueLong = Int64.Parse(inputLong);
            Console.WriteLine("'{0}' Int64.Parse Long value -{1}", inputLong, valueLong);
            //Exception value 
            string inputExc = "stringexception";
            long value = Int64.Parse(inputExc);
            Console.WriteLine("'{0}' Int64.Parse Long value -{1}", inputExc, value);
        } catch (FormatException e) {
            Console.WriteLine(e);
        }
    }
}

Output 

'904' Convert.ToInt16 -904
'12345' Convert.ToInt32 -12345
'21345678904' Convert.ToInt64 -21345678904
System.FormatException: Input string was not in a correct format.

Convert class

Convert is a static class present in system namespace which is used to convert string value to numeric values. This method will return Zero if given an input value null. This also can convert different bit integer values as given below.

  • Convert.ToInt16()
  • Convert.ToInt32()
  • Convert.ToInt64()
class Program {
    public static void Main() {
        try {
            //Parsing different values
            string inputShort = "904";
            string inputInt = "12345";
            string inputLong = "21345678904";
            //Exception value 
            string inputExc = "stringexception123";
            string s3 = null;
            //convert.class
            Console.WriteLine("'{0}' Convert.ToInt16 -{1}", inputShort, Convert.ToInt16(inputShort));
            Console.WriteLine("'{0}' Convert.ToInt32 -{1}", inputInt, Convert.ToInt32(inputInt));
            Console.WriteLine("'{0}' Convert.ToInt64 -{1}", inputLong, Convert.ToInt64(inputLong));
            Console.WriteLine(Convert.ToInt64(s3));
            Console.WriteLine("'{0}' Convert.ToInt64 -{1}", inputLong, Convert.ToInt64(inputExc));
        } catch (FormatException e) {
            Console.WriteLine(e);
        }
    }
}

Output 

'904' Convert.ToInt16 -904
'12345' Convert.ToInt32 -12345
'21345678904' Convert.ToInt64 -21345678904
0
System.FormatException: Input string was not in a correct format.

TryParse Method 

The TryParse() method returns the boolean values. Also available for 16, 32, and 64-bit signed integers values of converting string to numeric.

  • Int16.TryParse()
  • Int32.TryParse()
  • Int64.TryParse()

If we try to convert a different format or invalid values like string to integer it never throws an exception. Just returns false if cannot parse to an integer.

class Program {
    public static void Main() {
        string inputInt = "12345";
        string inputExc = "stringexception123";
        int number;
        bool isParsable = Int32.TryParse(inputInt, out number);
        Console.WriteLine("TryParse=> '{0}'", inputInt);
        if (isParsable) Console.WriteLine(number);
        else Console.WriteLine("Could not be parsed.");
        bool isParsable1 = Int32.TryParse(inputExc, out number);
        Console.WriteLine("TryParse=> '{0}'", inputExc);
        if (isParsable1) Console.WriteLine(number);
        else Console.WriteLine("Could not be parsed.");
    }
}

Output

TryParse=> '12345'
12345
TryParse=> 'stringexception123'
Could not be parsed.


Recommended Free Ebook
Similar Articles