All of the Convert.ToX
functions that accept an argument of type string
ultimately call down to the Parse
method of the appropriate datatype anyway.
For example, Convert.ToInt32(string)
looks something like this:
public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}
The same thing for all of the other numeric conversion methods, including Decimal
and DateTime
. So it's fairly irrelevant which one you use; the result (and speed) will be the same in either case.
Really, the only difference is the if (value == null)
guard clause at the beginning. Whether or not that's convenient depends on the specific use case. Generally, if you know that you have a non-null string
object, you might as well use Parse
. If you're not sure, ConvertToX
is a safer bet, requring less null-checking code inline.