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); } } }
1. | Converts string to integer | ||
2. | Int32.TryParse Method (String, Int32) | ||
3. | Read an input from console and convert to integer |