Have you ever tried adding 2 shorts together?
Did you know that a = a + b and a += b are different? I didn’t!
A short is an alias of Int16, or a number that fits into 16 bits. This is half the size of an integer, which is an alias for Int32.
short.MinValue = -32768 short.MaxValue = 32767
As a simple example, I want to add two shorts with values of 1 & 2 and assign it into a third short. But…The example below won’t compile!
short value1 = 1; short value2 = 2; short value3 = value1 + value2;
Why? Because adding two shorts returns an integer, not a short. Adding 2 integers together is valid, but not 2 shorts.
Ok, so what if we add the 2 shorts and then cast the integer to a short, like this:
short answer = 0; short value1 = 1; short value2 = 2; answer = (short) value1 + value2;
Nope, that won’t compile either. So how can you add 2 shorts?
It turns out that you can add them like this:
short value1 = 1; short value2 = 2; value1 += value2;
I don’t know why this is the case, but it is a quirk of the way the C# language works.