在那里,您可以使用和C#
将字符串转换为 Int32 。他们之间有什么区别?哪个表现更好?我应该在哪些情况下使用over ,反之亦然?Int32.Parse
Convert.ToInt32
Convert.ToInt32
Int32.Parse
4 回答
Basically Convert.ToInt32
uses 'Int32.Parse' behind scenes but at the bottom line
Convert.ToInt32
A null
will return 0. while in Int32.Parse
an Exception will be raised.
Int32.Parse (string s) method converts the string representation of a number to its 32-bit signed integer equivalent. When s is a null reference, it will throw ArgumentNullException.
whereas
Convert.ToInt32(string s) method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse () method. When s is a null reference, it will return 0 rather than throw ArgumentNullException.
Convert.ToInt32 (string value)
From MSDN:
Returns a 32-bit signed integer equivalent to the value of value. -or- Zero if value is a null reference (Nothing in Visual Basic).
The return value is the result of invoking the Int32.Parse
method on value.