int.Parse()和Convert.Toint()之间的任何性能差异? [英] Any performance difference between int.Parse() and Convert.Toint()?

查看:244
本文介绍了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);
}

这篇关于int.Parse()和Convert.Toint()之间的任何性能差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆