C# Reference ยท Keywords and Statements
checked and unchecked
Reference for C# checked and unchecked: control integer overflow, use both statements and expressions, and learn which operations are affected.
checked and unchecked decide what happens when integer arithmetic produces a result that does not fit in its type. In a checked context, the operation throws an OverflowException. In an unchecked context, the result silently wraps around, keeping only the low bits that fit.
By default, ordinary integer arithmetic on non-constant values is unchecked. Constant expressions that the compiler can evaluate, however, are always checked at compile time, so int x = int.MaxValue + 1; is a compile error unless you wrap it in unchecked. You can write both keywords as a block statement or as an expression.
int a = int.MaxValue; int wrapped = unchecked(a + 1); // -2147483648 int safe = checked(a + 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 { statements } unchecked { statements } checked(expression) unchecked(expression)
There is also a compiler switch, -checked, which makes the default context checked for the whole project (the CheckForOverflowUnderflow project property). An explicit unchecked still overrides it.
What is affected: ++, --, unary -, binary +, -, *, / on integral types, and explicit numeric conversions between integral types (and from float or double to an integral type).
Errors / undefined behavior: a checked operation throws System.OverflowException. Unchecked overflow is well defined (two's-complement wraparound); it is not undefined behavior.
Usage notes
- Scope is lexical.
checked { ... }andchecked(expr)affect only operations written inside them. A method called from inside the block runs under its own context. - Floating-point is unaffected.
floatanddoubleoverflow to infinity, never to an exception. Converting adoubletointin a checked context does throw when the value is out of range. - Constants: if the compiler can compute the value, overflow is reported as error CS0220 in the default context. Use
unchecked(...)when you really intend to wrap. decimalalways throws on overflow, regardless of context.- Library code:
Math.Abs(int.MinValue)throws, while-int.MinValuewraps in an unchecked context. For money, sizes and indexes, prefer checked arithmetic or a wider type. - Performance: checked arithmetic adds an overflow test. The cost is small, but measure it in hot loops before turning it on globally.
- Operators: user-defined
checkedoperators (public static T operator checked +(T a, T b)) were added in C# 11 so custom numeric types can follow the context.
Related
op-arithmetic,op-incrementandop-bitwisekw-implicit-explicitfor conversions- Tutorial: Integers