Addition on anything less than a int
is done in int
. This is described in the Standard ECMA-334 C# specification under Numeric promotions
12.4.7 Numeric promotions
When overload resolution rules (§12.6.4) are applied to this set of
operators, the effect is to select the first of the operators for
which implicit conversions exist from the operand types. [Example: For
the operation b * s, where b is a byte and s is a short, overload
resolution selects operator *(int, int) as the best operator. Thus,
the effect is that b and s are converted to int, and the type of the
result is int. Likewise, for the operation i * d, where i is an int
and d is a double, overload resolution selects operator *(double,
double) as the best operator. end example]
Additionally, if you want to catch overflows you will need to use the checked
keyword
Example
byte a = byte.MaxValue;
byte b = 1;
byte c = checked((byte)(a + b));
Additional Resources
checked (C# Reference)
By default, an expression that contains only constant values causes a
compiler error if the expression produces a value that is outside the
range of the destination type. If the expression contains one or more
non-constant values, the compiler does not detect the overflow.
Evaluating the expression assigned to i2 in the following example does
not cause a compiler error.