Holy cow, I wrote a book!
People complain that the following code elicits a warning:
byte b = 32; byte c = ~b; // error CS0029: Cannot implicitly convert type 'int' to 'byte'
"The result of an operation on 'byte' should be another 'byte', not an 'int'," they claim.
Be careful what you ask for. You might not like it.
Suppose we lived in a fantasy world where operations on 'byte' resulted in 'byte'.
byte b = 32; byte c = 240; int i = b + c; // what is i?
In this fantasy world, the value of i would be 16! Why? Because the two operands to the + operator are both bytes, so the sum "b+c" is computed as a byte, which results in 16 due to integer overflow. (And, as I noted earlier, integer overflow is the new security attack vector.)
Similarly,
int j = -b;
Is that really what you want?
Consider the following more subtle scenario:
struct Results { byte Wins; byte Games; }; bool WinningAverage(Results captain, Results cocaptain) { return (captain.Wins + cocaptain.Wins) >= (captain.Games + cocaptain.Games) / 2; }
In our imaginary world, this code would return incorrect results once the total number of games played exceeded 255. To fix it, you would have to insert annoying int casts.
return ((int)captain.Wins + cocaptain.Wins) >= ((int)captain.Games + cocaptain.Games) / 2;