I have seen three type of methods that are available in .NET to convert integer from string. Personally I feel, we can use int.TryParse(string s, out int).
The main difference between int.Parse(), Convert.ToInt32() and int.TryParse()
int.Parse(string str)
This method converts the string to integer.
If string str is either null, other than integer value or out of integer ranges, then it will throw ArgumentNullException, throw FormatException and throw OverflowException respectively.
Convert.ToInt32(string str)
This method converts the string to integer.
If string str is either null, other than integer value or out of integer ranges, then it will return 0 rather than throw ArgumentNullException, throw FormatException and throw OverflowException respectively.
int.TryParse(string str,Out)
This method converts the string to integer out variable and returns true if successfully parsed, otherwise false.
If string str is either null, other than integer value or out of integer ranges, then the out variable will have 0 rather than throw ArgumentNullException, FormatException and OverflowException respectively.
0 Comment(s)