Convert.ToDouble() or (double) ?

  • Thread starter Thread starter ORC
  • Start date Start date
O

ORC

What is the difference between:
Convert.ToDouble(Int16number);
and:
(double)Int16number; ?
and which is the fastest?

Thanks
Ole
 
Hi Ole,

Well, one performs a simple type conversion, the other uses the Convert
class to do the same.
Without having tested it I'd say (double)Int16 would be the fastest simply
because there is no calling of another method. In any case since you are
converting to a wider type there isn't actually much going on and these to
lines are equal

short s = 16;
double d = (double)s;
double d = s;

No cast is required since a double can hold all information from a short.
The other way around would require a cast.
 
Morten,

I compiled the two conversion methods and looked at the IL. You are right.
There is no function call with the cast, but there is with the convert.

Thomas P. Skinner [MVP]
 
Back
Top