Mega Code Archive

 
Categories / C# / Data Types
 

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);       }    } }