There is no addition operator for the short operator. The compiler automatically converts these values ββto int to complete the append. Therefore, the type of the expression x + y will be int . When assigning an int expression to a short variable, a cast is required. Like this:
short z = (short)(x + y);
Note The following information is usually not needed.
If you are concerned about overflow in a validated context, do the following:
short z = unchecked((short)(x + y));
This is usually not necessary, since unchecked is the default for most (or all) C # compilers, and this parameter is unlikely to ever change. If the assignment appears inside the checked statement, then presumably the person who writes the code knows what he is doing.
source share