The difference between Int32.TryParse() ConVert.ToInt32() Int32.Parse() in C# converts the string type to a number type

Understand Int32.TryParse() Turn on a blog

C# Int32.TryParse()

  1. Int32.TryParse() converts the string representation of a number to its equivalent 32-bit signed integer.
  2. Int32.Parse() converts the string representation of a number to its equivalent 32-bit signed integer.
int32.Parse(string)

If s is null. Throws ArgumentNullException

If the format of s is incorrect. Throw FormatException

If s represents a number less than MinValue or greater than MaxValue. OverflowException is thrown

3. Convert.ToInt32 () converts the specified value into a 32-bit signed integer. Not only is the character string converted to digits, there are many types of conversion.

Int32.Parse(String) 

If s is null. The return value is 0

If the format of s is incorrect. Throw FormatException

If s represents a number less than MinValue or greater than MaxValue. OverflowException is thrown

Compare the difference between Convert.ToInt32 and Int32.Parse()

When s is null, Int32.Parse() throws ArgumentNullException, Convert.ToInt() returns 0. When the Convert.ToInt32 parameter is "", an exception is thrown; when the int.Parse parameter is "", an exception is thrown.
The advantage of Int32.TryParse() is that it will not report an error.

Guess you like

Origin blog.csdn.net/wangwei021933/article/details/108491807