Convert.ToInt32和Int32.Parse有什么区别?

th1*_*ey3 11 c#

C#那里你可以使用Int32.Parse和两者将字符串转换为Int32 Convert.ToInt32.他们之间有什么区别?哪个表现更好?什么是我应该在哪里使用场景Convert.ToInt32过度Int32.Parse,反之亦然?

Tig*_*ran 18

如果你看看与反射器ILSpymscorlib你会看到下面的代码Convert.ToInt32

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

所以,在内部它使用int.Parse但使用CurrentCulture.实际上从代码中可以理解为什么当我指定null像参数时这个方法不会抛出异常.


ron*_*nen 8

基本上Convert.ToInt32在场景后面使用'Int32.Parse',但在底线 Convert.ToInt32A null将返回0.而在Int32.Parse异常中将引发.