0

Having

long number = 1475;

Is there any difference between

double d = Convert.ToDouble(number);

and

double d = (double)number;
ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58
John Smith1
  • 25
  • 1
  • 3
  • 10
    [Nope.](http://referencesource.microsoft.com/#mscorlib/system/convert.cs,1579) (Note that you can just write `double d = number;` as there's an implicit conversion from `long` to `double`.) – Jon Skeet Sep 15 '16 at 11:08
  • 1
    Lol double d ;) – Botonomous Sep 15 '16 at 11:09
  • 1
    @JonSkeet, why did you put your answer as a comment and not as a proper answer? – kiziu Sep 15 '16 at 11:11
  • Is `long` a 64 bit type in C#? If so, then note that the conversion to `double` is lossy for large numbers. – Bathsheba Sep 15 '16 at 11:12
  • Double is 16 bytes and longs are only 8... – Botonomous Sep 15 '16 at 11:13
  • "Cast when it's really a type of double, Convert when it's not a double but you want it to become one." – Matt Wilko Sep 15 '16 at 11:13
  • 1
    Convert can do more specific operations while converting than directcast due to the specific constructors. Here is a good post you can read more about: http://stackoverflow.com/questions/15394032/difference-between-casting-and-using-the-convert-to-method – Ricardo Pontual Sep 15 '16 at 11:14
  • @Botonomous: in which case, for example, 2**53 + 1 will not convert exactly to a double. (It will round to an even). – Bathsheba Sep 15 '16 at 11:14
  • 1
    @kiziu: When the answer is basically just a link, it doesn't feel worth having as a full answer... – Jon Skeet Sep 15 '16 at 11:15
  • @kiziu: i'm not sure if this is a new question, there are already similars/duplicates like [this](http://stackoverflow.com/questions/14860416/why-c-sharp-does-not-require-explicit-casting-for-convert-long-to-double) or [this](http://stackoverflow.com/questions/15394032/difference-between-casting-and-using-the-convert-to-method). – Tim Schmelter Sep 15 '16 at 11:15
  • @kiziu probably because this question won't last long here. (duplicate, lack of research, etc...) – Mixxiphoid Sep 15 '16 at 11:16

0 Answers0