最好使用int.Parse或Convert.ToInt32 [重复]

时间:2023-01-03 23:32:09

Possible Duplicate:
Whats the main difference between int.Parse() and Convert.ToInt32

可能重复:什么是int.Parse()和Convert.ToInt32之间的主要区别

Hi guys, I would like to know what are PRO and CONS of using Convert.ToInt32 VS int.Parse. Thanks for you support!

嗨伙计们,我想知道什么是使用Convert.ToInt32 VS int.Parse的PRO和CONS。感谢你的支持!

Here an example of syntax I am using

这是我正在使用的语法示例

            int myPageSize = Convert.ToInt32(uxPageSizeUsersSelector.SelectedValue);

            int myPageSize = int.Parse(uxPageSizeUsersSelector.SelectedValue);

I also found out this article, maybe can help for a discussion http://dotnetperls.com/int-parse http://aspdotnethacker.blogspot.com/2010/04/difference-between-int32parsestring.html http://aspdotnethacker.blogspot.com/p/visual-studio-performance-wizard.html

我也发现了这篇文章,也许可以帮助讨论http://dotnetperls.com/int-parse http://aspdotnethacker.blogspot.com/2010/04/difference-between-int32parsestring.html http:// aspdotnethacker .blogspot.com / p /视觉工作室 - 性能 - wizard.html

4 个解决方案

#1


25  

Convert.ToInt32 is for dealing with any object that implements IConvertible and can be converted to an int. Also, Convert.ToInt32 returns 0 for null, while int.Parse throws a ArgumentNullException.

Convert.ToInt32用于处理任何实现IConvertible的对象,并且可以转换为int。此外,Convert.ToInt32为null返回0,而int.Parse抛出ArgumentNullException。

int.Parse is specifically for dealing with strings.

int.Parse专门用于处理字符串。

As it turns out, the string type's IConvertible implementation merely uses int.Parse in its ToInt32 method.

事实证明,字符串类型的IConvertible实现仅在其ToInt32方法中使用int.Parse。

So effectively, if you call Convert.ToIn32 on a string, you are calling int.Parse, just with slightly more overhead (a couple more method calls).

如此有效,如果在字符串上调用Convert.ToIn32,则调用int.Parse,只需稍微增加开销(多一些方法调用)。

This is true for any conversion from string to some primitive type (they all call Parse). So if you're dealing with strongly-typed string objects (e.g., you're parsing a text file), I'd recommend Parse, simply because it's more direct.

对于从字符串到某种原始类型的任何转换(它们都调用Parse)都是如此。因此,如果您正在处理强类型字符串对象(例如,您正在解析文本文件),我建议使用Parse,因为它更直接。

Converting arbitrary objects (returned to you by some external library, for instance) is the scenario where I'd opt for using the Convert class.

转换任意对象(例如,通过某些外部库返回给你)是我选择使用Convert类的场景。

#2


17  

There's not much difference. Here's a quote found on msdn.

没有太大的区别。这是在msdn上找到的引用。

Basically the Convert class makes it easier to convert between all the base types.

基本上,Convert类可以更容易地在所有基类型之间进行转换。

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. MSDN

下面的Convert.ToInt32(String,IFormatProvider)调用Int32.Parse。所以唯一的区别是,如果将null字符串传递给Convert,则返回0,而Int32.Parse抛出ArgumentNullException。 MSDN

#3


3  

I cannot answer based on performance, but my preferred method is always int.tryparse(mystring, out myint) as that gives a clean failure that you can test for in the program flow (rather than doing a try/catch).

我无法根据性能回答,但我首选的方法总是int.tryparse(mystring,out myint),因为它会给出一个干净的失败,你可以在程序流程中测试(而不是做一个try / catch)。

#4


2  

The Convert interface is a more general purpose one. The net result should be the same though.

Convert接口是一个更通用的接口。最终结果应该是相同的。

Internally, it just calls int.Parse:

在内部,它只调用int.Parse:

public static int ToInt32(String value) {
    if (value == null)
        return 0;
    return Int32.Parse(value, CultureInfo.CurrentCulture);
}

Above code is from the reference source.

以上代码来自参考源。

#1


25  

Convert.ToInt32 is for dealing with any object that implements IConvertible and can be converted to an int. Also, Convert.ToInt32 returns 0 for null, while int.Parse throws a ArgumentNullException.

Convert.ToInt32用于处理任何实现IConvertible的对象,并且可以转换为int。此外,Convert.ToInt32为null返回0,而int.Parse抛出ArgumentNullException。

int.Parse is specifically for dealing with strings.

int.Parse专门用于处理字符串。

As it turns out, the string type's IConvertible implementation merely uses int.Parse in its ToInt32 method.

事实证明,字符串类型的IConvertible实现仅在其ToInt32方法中使用int.Parse。

So effectively, if you call Convert.ToIn32 on a string, you are calling int.Parse, just with slightly more overhead (a couple more method calls).

如此有效,如果在字符串上调用Convert.ToIn32,则调用int.Parse,只需稍微增加开销(多一些方法调用)。

This is true for any conversion from string to some primitive type (they all call Parse). So if you're dealing with strongly-typed string objects (e.g., you're parsing a text file), I'd recommend Parse, simply because it's more direct.

对于从字符串到某种原始类型的任何转换(它们都调用Parse)都是如此。因此,如果您正在处理强类型字符串对象(例如,您正在解析文本文件),我建议使用Parse,因为它更直接。

Converting arbitrary objects (returned to you by some external library, for instance) is the scenario where I'd opt for using the Convert class.

转换任意对象(例如,通过某些外部库返回给你)是我选择使用Convert类的场景。

#2


17  

There's not much difference. Here's a quote found on msdn.

没有太大的区别。这是在msdn上找到的引用。

Basically the Convert class makes it easier to convert between all the base types.

基本上,Convert类可以更容易地在所有基类型之间进行转换。

The Convert.ToInt32(String, IFormatProvider) underneath calls the Int32.Parse. So the only difference is that if a null string is passed to Convert it returns 0, whereas Int32.Parse throws an ArgumentNullException. MSDN

下面的Convert.ToInt32(String,IFormatProvider)调用Int32.Parse。所以唯一的区别是,如果将null字符串传递给Convert,则返回0,而Int32.Parse抛出ArgumentNullException。 MSDN

#3


3  

I cannot answer based on performance, but my preferred method is always int.tryparse(mystring, out myint) as that gives a clean failure that you can test for in the program flow (rather than doing a try/catch).

我无法根据性能回答,但我首选的方法总是int.tryparse(mystring,out myint),因为它会给出一个干净的失败,你可以在程序流程中测试(而不是做一个try / catch)。

#4


2  

The Convert interface is a more general purpose one. The net result should be the same though.

Convert接口是一个更通用的接口。最终结果应该是相同的。

Internally, it just calls int.Parse:

在内部,它只调用int.Parse:

public static int ToInt32(String value) {
    if (value == null)
        return 0;
    return Int32.Parse(value, CultureInfo.CurrentCulture);
}

Above code is from the reference source.

以上代码来自参考源。