Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The 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

The unchecked keyword can be applied as an operator to a single expression or as a statement block to encompass multiple operations.
// Operator syntax (single expression)
int expressionResult = unchecked(2147483647 + 1);

// Block syntax (multiple statements)
unchecked
{
    int maxValue = int.MaxValue;
    int wrappedResult = maxValue + 1;
}

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.
Note: The binary division operator (/) 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.
// Compile-time constant evaluation
// int defaultBehavior = 2147483647 + 1; // Generates Compiler Error CS0220

// Unchecked operator forces compilation and wraps the value
int uncheckedExpression = unchecked(2147483647 + 1); 
// Result: -2147483648 (Binary wrap-around)

unchecked
{
    byte maxByte = 255;
    // 255 + 2 = 257 (Binary: 00000001 00000001)
    // High-order bits are truncated to fit 8-bit byte
    byte truncatedResult = (byte)(maxByte + 2); 
    // Result: 1 (Binary: 00000001)
}

Scope and Limitations

  • Lexical Scoping: The unchecked context 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 unchecked keyword has no effect on float or double types, which handle overflows by returning Infinity or -Infinity according to IEEE 754 standards.
  • Decimal Type: The decimal type is immune to the unchecked context. Arithmetic operations on decimal values will always throw an OverflowException if the boundaries are exceeded, regardless of the presence of an unchecked block.
Master C# with Deep Grasping Methodology!Learn More