I came across the fact that bitwise or arithmetic operations are not allowed for primitive types less than 32 bits in size, but the corresponding assignment operations are actually allowed:
short BitwiseAnd(short a, short b) { return a & b; }
short BitwiseAndAssignment(ref short a, short b) { a &= b; }
short Add(short a, short b) { return a + b; }
short AddAssignment(ref short a, short b) { a += b; }
The same behavior holds for other short primitive types, such as byte, sbyteand ushort.
I understand that arithmetic and logical operations are defined for 32-bit and larger types ( int, long...), because this is what the processor provides ( see this question ), and the shorter types are extended and can be dropped by 8 or 16 bits. However, is there a reason why this will work in an assignment statement? At first I assumed that behind the scenes it is shortthrown at int, but then you will have an assignment / return value short value = (some int)that should give an error, since the cast is not implicit.
In another note: I tried the code in the direct Visual Studio window, but much more code works there. The direct window probably does some implicit casting that would normally be explicit. For example, short a = (int)5;allowed in the direct window. In order not to help.
source
share