Re: why does adding 2 bytes together result in an int?



This is because the devleopers of the platform have decided that the return
type of the + operator for the byte type should be an integer. That makes
sense, because (1) the return type must be the same for all operations, and
(2) there is no guarantee that adding 2 or more byte values together will
not overflow the size of a byte, which is 256.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A brute awe as you,
a Metallic hag entity, eat us.


"moondaddy" <moondaddy@xxxxxxxxxxxxx> wrote in message
news:Ofz5SwhPGHA.2300@xxxxxxxxxxxxxxxxxxxxxxx
my understanding is that the max value of a byte is 255. Therefore, why
does the following code get a compile error?

byte val1 = 10;
byte val2 = 23;
byte ttl;
ttl = val1 + val2; //this line wont compile

compile error: Cannot implicitly convert type 'int' to 'byte'.

val1 + val2 = 33 which is still in the range of a byte. therefore, why do
I need to convert ttl into an int?

Thanks.




--
moondaddy@xxxxxxxxxxxxx



.



Relevant Pages