3

Possible Duplicates:
difference between Convert.ToInt32 and (int)
Whats the main difference between int.Parse() and Convert.ToInt32

C#

Are there any differences between the type.Parse() methods and the methods at ConvertTo class?

Community
  • 1
  • 1
Gilad Naaman
  • 6,390
  • 15
  • 52
  • 82
  • 1
    possible duplicate of [difference between Convert.ToInt32 and (int)](http://stackoverflow.com/questions/1608801/difference-between-convert-toint32-and-int), [.Net Parse verses Convert](http://http://stackoverflow.com/questions/18465/net-parse-verses-convert), and [Whats the main difference between int.Parse() and Convert.ToInt32](http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32) – Justin Niessner Mar 30 '11 at 19:14
  • I would use type.TryParse because it's safer – Phil C Mar 30 '11 at 19:15
  • 1
    A more precise duplicate is '[Whats the main difference between int.Parse() and Convert.ToInt32](http://stackoverflow.com/questions/199470/whats-the-main-difference-between-int-parse-and-convert-toint32)'. – ladenedge Mar 30 '11 at 19:16

7 Answers7

3

No there is no substantial difference between Int32.Parse and Convert.ToInt32(string). The latter just forwards it's call to Int32.Parse

One minor difference though is that if you pass null to Convert.ToInt32 it will return 0, while Int32.Parse will through an ArgumentNullException

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
1

One notable difference is TypeConverter.ConvertTo(object) doesn't throw an exception when object is null, but type.parse does throws an exception when its contents are null

Sreedhar Danturthi
  • 7,119
  • 19
  • 68
  • 111
1

Most Important Difference is:

  • -Convert.ToInt32(s) doesn't throw an exception when s is null, but Parse() does
  • If you're collecting input from a user, you'd generally user Int32.TryParse()

  • Int32.Parse() and Int32.TryParse() can only convert strings. Convert.ToInt32() can take any class that implements IConvertible.

  • Basically in all truth, When you look at the source from TryParse it actually has no exception handling at all - just character manipulation and bit shifting

  • For Performance details, read this post: http://blogs.msdn.com/b/ianhu/archive/2005/12/19/505702.aspx

vinayvasyani
  • 1,393
  • 4
  • 13
  • 34
0

AFAIK, int.Parse() is inherited from the Number class, where it's defined. Convert.ToIn32() implements the IConvertable interface and gets the type of the object you pass before handling the conversion.

Either way I think it ends up at Int32.Parse anyway.

dotalchemy
  • 2,459
  • 19
  • 24
0

There are some differences, e.g. Convert can convert to Int32 from many objects (Int16, string, SByte, etc.). Also, Int32.Parse can accept NumberStyles, which Convert.ToInt32 cannot.

Other than that, Reflector (http://reflector.red-gate.com) has this to say:

class Convert:

public static int ToInt32(string value)
{
    if (value == null)
    {
        return 0;
    }
    return int.Parse(value, CultureInfo.CurrentCulture);
}
telewin
  • 1,102
  • 8
  • 17
0

Here's the code retrieved using Reflector.

Int32.Parse:

public static int Parse(string s)
{
    return 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.ToInt32:

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

So, Convert anyway calls methods of Int32. Using of Int32.Parse will be more efficiently, but you need take into account that it can throw exception on passing null argument.

kyrylomyr
  • 12,192
  • 8
  • 52
  • 79
0

Converting or direct casting can cause data loss. You should consider making use of "is" and "as" over casting or converting. In addition to protection against data loss, these are both more efficient at runtime.

As Austin stated, if you are going to parse, use tryparse and handle failures accordingly.

dbobrowski
  • 846
  • 2
  • 9
  • 18