int.Parse()和Convert.Toint()之间的任何性能差异?

int.Parse()和Convert.Toint()之间的任何性能差异?

问题描述:

是否有一个字符串转换为一个整数值int.Parse()和Convert.ToInt32()之间的任何显著优势?

Is there any significant advantages for converting a string to an integer value between int.Parse() and Convert.ToInt32() ?

string stringInt = "01234";

int iParse = int.Parse(stringInt);

int iConvert = Convert.ToInt32(stringInt);



我发现的问题问铸造VS转换,但我认为这是不同的,对不对?

I found a question asking about casting vs Convert but I think this is different, right?

在传递两个字符串作为参数,调用Convert.ToInt32内部int.Parse。所以,唯一的区别是一个额外的空检查。

When passed a string as a parameter, Convert.ToInt32 calls int.Parse internally. So the only difference is an additional null check.

下面是从.net反射代码

Here's the code from .NET Reflector

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