Skip to content
Tutorials Center
My learning

Topics

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.

C#
int big = int.MaxValue;
int wrapped = unchecked(big + 1);     // -2147483648
int safe = checked(big + 1);          // throws OverflowException

Try it

C# Wraparound and checked arithmetic

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.

C# Safe patterns

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

C#
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 unchecked code, and hash functions and checksums rely on it. Mark such code unchecked explicitly so readers know it is intentional.
  • Turn checking on for business code (money counts, quantities, indexes) with the project setting, and use unchecked blocks 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, converting long to int for array sizes, and Length - 1 on unsigned values.
  • Unsigned types wrap downward. uint x = 0; x--; gives 4294967295 in unchecked code.
  • Overflow can change the sign. After wrapping, a comparison like a + b < a can be used to detect it, but it is clearer to use checked, a wider type, or Math.BigMul.
  • nint/nuint, Int128 and BigInteger. Use System.Numerics.BigInteger when values may exceed 64 bits.
  • Shifts. Shift counts are masked (1 << 33 equals 1 << 1 for int), and shifting does not throw on overflow.