Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Operators and Expressions

The checked and unchecked operators

Reference for C# checked and unchecked operators: control integer overflow in expressions, how constants differ, and what the compiler option changes.

Integer arithmetic in C# can overflow: adding one to int.MaxValue does not fit in 32 bits. What happens next depends on the context. In a checked context the runtime throws OverflowException. In an unchecked context the result silently wraps around.

Normal code runs unchecked unless you turn on the compiler option CheckForOverflowUnderflow. The checked(...) and unchecked(...) operators, and the matching statement blocks, let you choose for a single expression or a few lines.

C#
int a = int.MaxValue;
int b = checked(a + 1);    // throws OverflowException
int c = unchecked(a + 1);  // -2147483648

Try it

C# Overflow in both contexts

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# Blocks and compile-time constants

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)
unchecked(expression)

checked { statements }
unchecked { statements }
Name Description
expression An expression of an integral type. Only arithmetic and conversions written directly inside it are affected.

Returns: the value of the expression, or in a checked context an exception instead of a wrapped value.

Errors: OverflowException at run time in a checked context. A constant expression that overflows is a compile-time error (CS0220) unless it is inside unchecked.

Usage notes

  • The operators affect +, -, *, unary -, ++, -- and explicit integer conversions. They do not affect / by zero (always DivideByZeroException) or floating-point math (which gives infinity).
  • The context applies to the code written inside the parentheses or block. It does not reach into methods called from there.
  • Add <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> to the project file to make everything checked by default. Then unchecked is how you opt out.
  • int.MinValue / -1 and Math.Abs(int.MinValue) throw regardless of the context.
  • Hash functions and random number generators rely on wrap-around, so mark them unchecked.
  • Checked arithmetic is slightly slower because the processor must test the overflow flag after each operation. Measure before turning it on globally in hot paths.
  • For conversions between numeric types, int.CreateChecked(value) throws on overflow and int.CreateSaturating(value) clamps to the nearest limit.
  • decimal always throws on overflow, regardless of the context.