什么是Convert.ToInt32和Int32.Parse之间的区别? [英] What's the difference between Convert.ToInt32 and Int32.Parse?
问题描述
在 C#
那里,你可以同时使用 Int32.Parse
和 Convert.ToInt32
。什么是它们之间的区别?哪个性能更好?什么是我应该在哪里使用场景 Convert.ToInt32
在 Int32.Parse
,反之亦然?
In C#
there you can convert a string to Int32 using both Int32.Parse
and Convert.ToInt32
. What's the difference between them? Which performs better? What are the scenarios where I should use Convert.ToInt32
over Int32.Parse
and vice-versa?
推荐答案
Int32.Parse(字符串s)
方法将数字转换到32位有符号整数等效字符串表示。
Int32.parse(string)
Int32.Parse(string s)
method converts the string representation of a number to its 32-bit signed integer equivalent.
- 在
取值
为空引用,将扔ArgumentNullException
- 如果
取值
为不是整数,其他值,将投FormatException
- 在
取值
表示比MINVALUE
MaxValue的或更大> 它将投发生OverflowException
- When
s
is a null reference, it will throwArgumentNullException
. - If
s
is other than integer value, it will throwFormatException
. - When
s
represents a number less thanMinValue
or greater thanMaxValue
, it will throwOverflowException
.
例如对于
string s1 = "1234";
string s2 = "1234.65";
string s3 = null;
string s4 = "123456789123456789123456789123456789123456789";
int result;
bool success;
result = Int32.Parse(s1); //-- 1234
result = Int32.Parse(s2); //-- FormatException
result = Int32.Parse(s3); //-- ArgumentNullException
result = Int32.Parse(s4); //-- OverflowException
Convert.ToInt32(串)
Convert.ToInt32(字符串s)
方法转换的32位有符号整数等效指定的字符串表示。这依次调用 Int32.Parse()
方法。
Convert.ToInt32(string)
Convert.ToInt32(string s)
method converts the specified string representation of 32-bit signed integer equivalent. This calls in turn Int32.Parse()
method.
- 在
取值
为空引用,它将返回0 ,而不是抛出ArgumentNullException
。 - 如果
取值
为不是整数,其他值,将扔FormatException
- 当
取值
表示的小于许多MINVALUE
或大于的MaxValue
,将扔发生OverflowException
- When
s
is a null reference, it will return 0 rather than throwArgumentNullException
. - If
s
is other than integer value, it will throwFormatException
. - When
s
represents a number less thanMinValue
or greater thanMaxValue
, it will throwOverflowException
.
例如:
result = Convert.ToInt32(s1); //-- 1234
result = Convert.ToInt32(s2); //-- FormatException
result = Convert.ToInt32(s3); //-- 0
result = Convert.ToInt32(s4); //-- OverflowException
Int32.TryParse(字符串,OUT INT)
Int32.Parse(字符串,OUT INT)
方法转换的32位有符号整数相当于out变量指定的字符串表示,如果它被成功解析,否则为false返回true。这种方法是在C#2.0中可用。
Int32.TryParse(string, out int)
Int32.Parse(string, out int)
method converts the specified string representation of 32-bit signed integer equivalent to out variable, and returns true if it is parsed successfully, false otherwise. This method is available in C# 2.0.
- 在
取值
为空引用,将返回0 ,而不是抛出ArgumentNullException
。 - 如果
取值
为不是整数其他值,输出变量的有0 ,而不是FormatException
。 - 在
取值
表示比MINVALUE
少一个数字或更大比MaxValue的
,输出变量将有0 而非发生OverflowException
。
- When
s
is a null reference, it will return 0 rather than throwArgumentNullException
. - If
s
is other than an integer value, the out variable will have 0 rather thanFormatException
. - When
s
represents a number less thanMinValue
or greater thanMaxValue
, the out variable will have 0 rather thanOverflowException
.
例如:
success = Int32.TryParse(s1, out result); //-- success => true; result => 1234
success = Int32.TryParse(s2, out result); //-- success => false; result => 0
success = Int32.TryParse(s3, out result); //-- success => false; result => 0
success = Int32.TryParse(s4, out result); //-- success => false; result => 0
结论
Convert.ToInt32()
比 Int32.Parse()
,因为它返回0更好而不是例外。但同样,根据需要,这可以被使用。 的TryParse
将是最好的,因为它总是自行处理异常。
Conclusion
Convert.ToInt32()
is better than Int32.Parse()
since it returns 0 rather than an exception. But again, according to the requirement, this can be used. TryParse
will be the best since it always handles exceptions by itself.
这篇关于什么是Convert.ToInt32和Int32.Parse之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!