C# 8- and 16-Bit Integrals and Probable Compile-Time Error

The 8- and 16-bit integral types are byte, sbyte, short, and ushort.These types lack their own arithmetic operators! so C# implicitly converts them to larger types as required. This can cause a compile-time error when trying to assign the result back to a small integral type:

short x = 1, y = 1;
short z = x + y; // Compile-time error

In this case, x and y are implicitly converted to int so that the addition can be per‐formed. This means the result is also an int, which cannot be implicitly cast back to a short (because it could cause loss of data). To make this compile, we must add an explicit cast:

short z = (short) (x + y); // OK









To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics