Int32.Parse() VS Convert.ToInt32()?

They are exactly the same, except that Convert.ToInt32(null) returns 0.

Convert.ToInt32 is defined as follows:

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

Well, Reflector says...

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

public static int Parse(string s)
{
    return Number.ParseInt32(s, NumberStyles.Integer, NumberFormatInfo.CurrentInfo);
}

So they're basically the same except that Convert.ToInt32() does an added null check.

Tags:

C#