This is a basic question. What is the difference between casting and using
the Convert.ToXXX methods, from the standpoint of the compiler, in terms of
performance, and in other ways? e.g.
this.ContentID = (int)ci.Conid;
vs.
this.ContentID = Convert.ToInt32(ci.Conid);
I tend to use the latter form because it seems more descriptive to me, but
it would be good to know what's best practice. I'm guessing those methods
are more expensive computationally.
-KF
Sherif Elmetainy - 27 Aug 2007 18:47 GMT
The difference it terms of functionality is shown in the following examples:
object a = "45"; // a is of type String
int b = (int)a; // InvalidCastException is thrown
int c= Convert.ToInt32(a); // c will contain 45
string d = "45";
int e = (int)d; // compiler error
int f = Convert.ToInt32(d); // f will contain 45
string g = "test";
int h = Convert.ToInt32(g); // FormatException is thrown because
Convert.ToInt32 will attempt to call Int32.Parse
long i = long.MaxValue;
unchecked
{
int j = (int)i; // OverflowException is thrown;
int k = Convert.ToInt32(i); // OverflowException is thrown
}
checked
{
int l = (int)d; // f will contain -1
int m = Convert.ToInt32(i); // OverflowException is thrown
(checked/unchecked doesn't affect Convet.ToXXX methods)
}
Depending on which overload of Convert.ToXXX you are calling, the
performance can be a bit slower than type casting (in case of the overload
that takes an object as a parameter) and functionality can be different, but
in many cases performance should be the same assuming that the JIT compiler
will inline the Convert.ToXXX calls.
Regards,
Sherif
> This is a basic question. What is the difference between casting and using
> the Convert.ToXXX methods, from the standpoint of the compiler, in terms
[quoted text clipped - 9 lines]
>
> -KF
james - 27 Aug 2007 19:00 GMT
Sherif,
I think checked and unchecked are backwards :)
System.Convert methods also perform rounding. For instance here is
some disassembly
public static int ToInt32(double value)
{
if (value >= 0)
{
if (value < 2147483647.5)
{
int num = (int) value;
double num2 = value - num;
if ((num2 > 0.5) || ((num2 == 0.5) && ((num & 1) != 0)))
{
num++;
}
return num;
}
}
else if (value >= -2147483648.5)
{
int num3 = (int) value;
double num4 = value - num3;
if ((num4 < -0.5) || ((num4 == -0.5) && ((num3 & 1) != 0)))
{
num3--;
}
return num3;
}
throw new
OverflowException(Environment.GetResourceString("Overflow_Int32"));
}
-James
Sherif Elmetainy - 27 Aug 2007 22:58 GMT
Yes, you are right :) Sorry about that.
> Sherif,
>
[quoted text clipped - 33 lines]
>
> -James
pedrito - 27 Aug 2007 18:58 GMT
Well, casting is done by the compiler itself whereas Convert.To.... is a
method call. Performance-wise, the cast is generally preferred.
Convert.To, I find, is generally more handy for converting strings to
numbers. I rarely use it for anything else. I don't know that I've ever used
it to cast from one number to another. I don't really see that there's any
advantage and the casting looks cleaner and easier to read, in my opinion.
> This is a basic question. What is the difference between casting and using
> the Convert.ToXXX methods, from the standpoint of the compiler, in terms
[quoted text clipped - 9 lines]
>
> -KF
Ken Fine - 29 Aug 2007 05:18 GMT
Thanks everyone! I appreciate the detailed examples, very succinct and
clear.
-KF
> Well, casting is done by the compiler itself whereas Convert.To.... is a
> method call. Performance-wise, the cast is generally preferred.
[quoted text clipped - 18 lines]
>>
>> -KF
Hilton - 16 Sep 2007 22:33 GMT
(int) <double> is a LOT faster than Convert.ToInt32 (<double>)
YMMV with other types of parameters.
Hilton