rbwhitaker / CSharpPlayersGuideEarlyAccess

A place to track issues with the C# Player's Guide for patches and future editions
18 stars 0 forks source link

Addition between an int and a double? #711

Closed cuneyt76 closed 7 months ago

cuneyt76 commented 7 months ago

Hello, From page 58: "For example, addition is defined between two ints and two doubles but not between an int and a double." Is there something wrong with the sentence above? Isn't it perfectly legal to add int a and double b as in:

int a = 5;
double b = 6.5;
Console.WriteLine(a + b);             // 11.5
rbwhitaker commented 7 months ago

Ah, yes! But that's because the compiler inserts an automatic conversion from int to double, and the actual addition is done between two double values, with a double result.

cuneyt76 commented 7 months ago

Hm, understood. Thanks for the inner details. But still, the sentence, in its current form, might be a bit confusing / might require additional explanation especially for beginners. When I first read it, I thought [although my intuition told me otherwise] it effectively said you could not add an int and a double (and expected to see an error when I attempted to do so -similar to the one we encounter when we attempt to add an int and, say, a string).