What is the output of the following code?
using System; class MainClass { public static void Main(string[] args) { short x = 1, y = 1; short z = x + y; Console.WriteLine(x); Console.WriteLine(z); } }
short z = x + y; // Compile-time error
The 8- and 16-bit integral types are byte, sbyte, short, and ushort.
These types don't have their own arithmetic operators.
C# implicitly converts them to larger types during the arithmetic expression.
This can cause a compile-time error when assigning the result back to a small integral type:
You can use an explicit cast to fix the compile time error
short z = (short) (x + y); // OK