Skip to main content
1 of 2
Vikas Lalwani
  • 1.1k
  • 19
  • 34

All above answers are good but for information, we can use int.TryParse which is safe to convert string to int , example

// TryParse returns true if the conversion succeeded // and stores the result in j. int j; if (Int32.TryParse("-105", out j)) Console.WriteLine(j); else Console.WriteLine("String could not be parsed."); // Output: -105 

TryParse never throws an exception—even on invalid input and null. It is overall preferable to int.Parse in most program contexts.

Source: How to convert string to int in C#? (With Difference between Int.Parse and Int.TryParse)

Vikas Lalwani
  • 1.1k
  • 19
  • 34