TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
checked keyword in C# explicitly enables arithmetic overflow checking for integral-type operations, enum operations, explicit numeric conversions, and user-defined types implementing checked operators. When an operation inside a checked context produces a result that exceeds the maximum or minimum bounds of the destination data type, the Common Language Runtime (CLR) throws a System.OverflowException instead of silently wrapping or truncating the value.
You can apply checked in two ways: as an operator for a single expression, or as a statement block for multiple expressions.
Operator Syntax:
Technical Mechanics
- Affected Types: The
checkedcontext applies to integral types (sbyte,byte,short,ushort,int,uint,long,ulong,char,nint, andnuint),enumtypes (which are backed by integral types), and user-definedstructorclasstypes that implementcheckedoperator overloads. - User-Defined Checked Operators (C# 11+): Developers can define custom overflow behavior for user-defined types using the
checkedmodifier on operator overloads. When an operation is invoked inside acheckedcontext, the compiler resolves to thecheckedvariant of the operator if it exists. Note that declaring acheckedoperator requires also declaring its matching non-checked version to avoid a compile-time error (CS9025).
- Floating-Point and Decimal Types: Floating-point arithmetic operations (
float,double) are unaffected bycheckedand resolve toInfinityon overflow. However, explicit casts from floating-point types to integral types are strictly evaluated for overflow within acheckedcontext. Thedecimaltype always throws anOverflowExceptionon arithmetic overflow, regardless of the checking context. - Affected Operations: The context enforces bounds checking on the following operations:
- Binary arithmetic operators:
+,-,* - Unary operators:
-,++,-- - Explicit numeric conversions (casts) between integral types or enums.
- Explicit numeric conversions (casts) from floating-point types (
float,double) to integral types. - User-defined operators explicitly marked with the
checkedmodifier.
- Binary arithmetic operators:
- Compile-Time vs. Runtime: By default, C# evaluates non-constant integral expressions at runtime in an
uncheckedcontext (unless the/checkedcompiler flag is enabled). Thecheckedkeyword forces the compiler to emit overflow-aware IL (Intermediate Language) instructions. For arithmetic, it emitsadd.ovf,sub.ovf, ormul.ovfinstead of the standardadd,sub, ormulinstructions. For explicit conversions, it emitsconv.ovf.*instructions (e.g.,conv.ovf.i4) instead of standard conversion instructions (e.g.,conv.i4). Constant expressions are always checked at compile-time, regardless of the presence of thecheckedkeyword, unless explicitly wrapped in anuncheckedblock.
Lexical Scoping Limitation
Thechecked environment is strictly lexically scoped. It only applies to the operations textually written within the parentheses or braces. It does not propagate down the call stack to methods invoked within the block.
Master C# with Deep Grasping Methodology!Learn More





