What is the main difference between int.Parse () and Convert.ToInt32 ()?

14

In C #, there are two ways (among others) to convert other types to int :

Convert.ToInt32(valor) and Int32.Parse(valor) .

What is the main difference between these two conversion methods?

    
asked by anonymous 06.10.2015 / 18:41

2 answers

11

The Int32.Parse(valor) only converts content from string . Convert.ToInt32() has overloads to work with various types. This is the main difference.

But the best way is to see how it is internally:

Parse:

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

internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
{
    byte* stackBuffer = stackalloc byte[1 * 0x72];
    NumberBuffer number = new NumberBuffer(stackBuffer);
    int num = 0;
    StringToNumber(s, style, ref number, info, false);
    if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
    {
        if (!HexNumberToInt32(ref number, ref num))
        {
            throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
        }
        return num;
    }
    if (!NumberToInt32(ref number, ref num))
    {
        throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
    }
    return num;
}

Convert:

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

Retired from this OS response .

But you can see the actual source code: Convert and Parse .

Prefer TryParse()

Related: What's the difference between using variable (int) or Convert.ToInt32 (variable)?

    
06.10.2015 / 18:47
9

Playing Int32.Parse() in Reflector :

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

Which in turn calls:

internal static unsafe int ParseInt32(string s, NumberStyles style, NumberFormatInfo info)
{
    byte* stackBuffer = stackalloc byte[1 * 0x72];
    NumberBuffer number = new NumberBuffer(stackBuffer);
    int num = 0;
    StringToNumber(s, style, ref number, info, false);
    if ((style & NumberStyles.AllowHexSpecifier) != NumberStyles.None)
    {
        if (!HexNumberToInt32(ref number, ref num))
        {
            throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
        }
        return num;
    }
    if (!NumberToInt32(ref number, ref num))
    {
        throw new OverflowException(Environment.GetResourceString("Overflow_Int32"));
    }
    return num;
}

Playing Convert.ToInt32() in Reflector:

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

Use

  • Int32.Parse() is noticeably faster than Convert.ToInt32() , but more insecure. You should use Int32.Parse() when you are sure that the input value is an integer;
  • There's still Int32.TryParse() , which is a bit more secure. Returns zero if the conversion fails and does not throw an exception;
  • Convert.ToInt32() is a slightly more secure envelope of Int32.Parse() (you can see through the code.

Performance

Still, the quickest way to do the conversion is by simple cast :

var inteiro = (int)meuObjeto;

Of course it is quite unsafe to do this, but it pays off if, again, you know that what you have inside object is an integer.

By this answer , you can see the assembly of this cast :

.locals init (
    [0] object x,
    [1] int32 Y)
L_0000: ldc.i4.1 
L_0001: box int32
L_0006: stloc.0 
L_0007: ldloc.0 
L_0008: unbox int32
L_000d: ldobj int32
L_0012: stloc.1 
L_0013: ret

While Convert.ToInt32() generates:

.locals init (
    [0] object x,
    [1] int32 Y)
L_0000: ldc.i4.1 
L_0001: box int32
L_0006: stloc.0 
L_0007: ldloc.0 
L_0008: call object [mscorlib]System.Runtime.CompilerServices.RuntimeHelpers::GetObjectValue(object)
L_000d: call int32 [mscorlib]System.Convert::ToInt32(object)
L_0012: stloc.1 
L_0013: ret

This response has a complete benchmark on all possible assignment modes for an integer . The difference is overwhelming.

    
06.10.2015 / 18:45