C# Reference ยท Types and Literals
Integer overflow
Reference for C# integer overflow: wraparound in unchecked code, checked and unchecked contexts, compiler options, saturating helpers and safe patterns.
Integer types have a fixed range. An int holds -2,147,483,648 to 2,147,483,647, so int.MaxValue + 1 does not fit. What happens next depends on the context: in an unchecked context the result silently wraps around (two's complement), and in a checked context the runtime throws OverflowException.
By default, arithmetic on non-constant values is unchecked, which is fast but makes overflow bugs silent. You opt in to detection with the checked keyword, either per expression or for a whole block.
int big = int.MaxValue; int wrapped = unchecked(big + 1); // -2147483648 int safe = checked(big + 1); // throws OverflowException
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
checked(expression) // expression form unchecked(expression) checked { statements } // block form unchecked { statements }
| Aspect | Behavior |
|---|---|
| Default context | unchecked for non-constant expressions. The project option CheckForOverflowUnderflow (/checked) makes it checked. |
| Constant expressions | Always checked at compile time: int.MaxValue + 1 is a compile error unless wrapped in unchecked(...). |
| Affected operations | +, -, *, unary -, ++, --, and explicit conversions between integer types, including compound assignments. |
| Not affected | Floating-point arithmetic (never throws; gives infinity or NaN), decimal (always throws on overflow), Math methods that document their own exceptions. |
| Division | int.MinValue / -1 throws OverflowException in every context, and division by zero throws DivideByZeroException. |
User-defined operators have their own checked variants since C# 11 (operator checked +), which the compiler selects inside checked contexts.
Errors. OverflowException ("Arithmetic operation resulted in an overflow.") from checked arithmetic and checked conversions. Math.Abs(int.MinValue) and checked(-int.MinValue) also throw because the positive value does not exist.
Usage notes
- Silent wraparound is not an error. It is well-defined behavior in
uncheckedcode, and hash functions and checksums rely on it. Mark such codeuncheckedexplicitly so readers know it is intentional. - Turn checking on for business code (money counts, quantities, indexes) with the project setting, and use
uncheckedblocks for the few algorithms that need wraparound. The cost is small on modern CPUs. - Common bugs. Averaging (
(a + b) / 2), size calculations (width * height * bytesPerPixel) before allocating, convertinglongtointfor array sizes, andLength - 1on unsigned values. - Unsigned types wrap downward.
uint x = 0; x--;gives4294967295in unchecked code. - Overflow can change the sign. After wrapping, a comparison like
a + b < acan be used to detect it, but it is clearer to usechecked, a wider type, orMath.BigMul. nint/nuint,Int128andBigInteger. UseSystem.Numerics.BigIntegerwhen values may exceed 64 bits.- Shifts. Shift counts are masked (
1 << 33equals1 << 1forint), and shifting does not throw on overflow.