The reason your first two samples do not compile is because:
- Casting binds βtougherβ than subtraction. That is: (C) de 'means' ((C) d) -e', not '(C) (de)'. The casting operator has a higher priority.
- Therefore, the type of both operands for subtraction is a byte, regardless of the cast.
- The subtraction type is int, because there is no subtraction operator in bytes.
- Therefore, you assign an int to a byte without a cast, which is illegal.
There is no subtraction operator in bytes because, suppose you have a byte containing 7 and you subtract a byte containing 8 from it, do you really want it to be byte 255? I think most people would like this to be int -1.
Finally, why do you do this in bytes in the first place? It makes no sense. Balls are not bytes in C #; if you want to do arithmetic on characters, then why not subtract
char 96 from char 'y' instead of doing a crash and dangerous conversion to bytes?
source share