开发者

Which conversion method is better?

Convert.ToInt32 or Int.Parse which is better and why? Is there any specific condition where i开发者_Go百科 can use these two?.


Presumably you're asking about the Convert.ToInt32 which takes a string. In that case, it simply calls int.Parse internally, so there's no real difference except that Convert gracefully handles null by returning 0.


Convert.ToInt32 internally calls Int.Parse with a null check. So the Null check is extra and does not throw in case of a Null parameter.

You can refer to this question here: Any performance difference between int.Parse() and Convert.Toint()?


One of them calls the other (though I can't remember which is which ATM), so there's no practical difference between them.


From Reflector:

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

So calling int.Parse() would be a little quicker.


If you don't expect null to ever be passed as the argument, use int.Parse, since then you'll be alerted via an exception when something does go wrong.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜