byte + byte = int... why?
- by Robert C. Cartaino
Looking at this C# code...
byte x = 1;
byte y = 2;
byte z = x + y; // ERROR: Cannot implicitly convert type 'int' to 'byte'
The result of any math performed on byte (or short) types is implicitly cast back to an integer. The solution is to explicitly cast the result back to a byte, so...
byte z = (byte)(x + y); // works
…