> ## 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.

# C# Unchecked

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.

```csharp theme={"dark"}
// 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.*

```csharp theme={"dark"}
// 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.

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor C# Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
