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?
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?
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
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
Most Important Difference is:
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
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.
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);
}
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.
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.