0

If i input through console window and want the input to be casted to int; should I use "int.Parse" or "Convert.ToInt32"?

Please explain the difference. i observed them in different programs.

9th Jan 2017, 4:31 PM
Prashant Brahmbhatt
Prashant Brahmbhatt - avatar
2 Respostas
+ 2
taken from stackoverflow: If you've got a string, and you expect it to always be an integer (say, if some web service is handing you an integer in string format), you'd use Int32.Parse(). If you're collecting input from a user, you'd generally use Int32.TryParse(), since it allows you more fine-grained control over the situation when the user enters in invalid input. Convert.ToInt32() takes an object as its argument, and I believe it invokes Int32.TryParse() when it finds that the object taken as the argument is a string. Convert.ToInt32() also does not throw ArgumentNullException when it's argument is null the way Int32.Parse() does. That also means that Convert.ToInt32() is probably a wee bit slower than Int32.Parse(), though in practice, unless you're doing a very large number of iterations in a loop, you'll never notice it.
9th Jan 2017, 4:34 PM
Nahuel
Nahuel - avatar
0
Both of int.Parse() and System. Convert, and System. BitConverter helper class can be used to attempt conversion non-convertible types, but be aware that passing these types may cause compilation error if conversion fails
9th Jan 2017, 4:40 PM
Alex Soh Chye Wat
Alex Soh Chye Wat - avatar