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.
unchecked keyword in C# is a context specifier that suppresses overflow-checking for integral-type arithmetic operations and conversions. When an operation produces a value outside the range of the destination type within an unchecked context, the runtime does not throw an OverflowException. Instead, it truncates the result by discarding the high-order bits that exceed the destination type’s memory allocation, resulting in a two’s complement wrap-around.
Syntax
Theunchecked keyword can be applied as an operator to a single expression or as a statement block to encompass multiple operations.
Technical Mechanics
By default, C# evaluates non-constant integral expressions at runtime in an unchecked context (unless the/checked compiler option is enabled). However, constant expressions evaluated at compile-time are checked by default and will generate compiler error CS0220 if an overflow occurs. The unchecked operator explicitly overrides this compile-time behavior for supported operations.
When applied, the unchecked context affects the following operations on integral types (sbyte, byte, short, ushort, int, uint, long, ulong, char, nint, nuint):
- Unary operators:
++,--,- - Binary operators:
+,-,* - Explicit numeric conversions between integral types.
/) is not affected by the unchecked context. Integer division overflow (such as int.MinValue / -1) will always throw an OverflowException at runtime or generate a compiler error at compile-time, regardless of the checking context.
Scope and Limitations
- Lexical Scoping: The
uncheckedcontext is strictly lexical. It only applies to the operations directly written within its parentheses or block. It does not propagate down the call stack to methods invoked within the block. - Floating-Point Types: The
uncheckedkeyword has no effect onfloatordoubletypes, which handle overflows by returningInfinityor-Infinityaccording to IEEE 754 standards. - Decimal Type: The
decimaltype is immune to theuncheckedcontext. Arithmetic operations ondecimalvalues will always throw anOverflowExceptionif the boundaries are exceeded, regardless of the presence of anuncheckedblock.
Master C# with Deep Grasping Methodology!Learn More





