Parse string to integer : int parse « Data Types « C# / C Sharp






Parse string to integer

  

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string string1 = "2";
      try {
         int number1 = Int32.Parse(string1);
         Console.WriteLine(number1);
      }
      catch (OverflowException) {
         Console.WriteLine("'{0}' is out of range of a 32-bit integer.", string1);
      }
      catch (FormatException) {
         Console.WriteLine("The format of '{0}' is invalid.", string1);
      }

   }
}

   
    
  








Related examples in the same category

1.Converts string to integer
2.Int32.TryParse Method (String, Int32)
3.Read an input from console and convert to integer