> ## 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# Post-Decrement

The `--` operator is a unary decrement operator that subtracts 1 (or the byte size of the type, in the case of pointers) from the value of its operand. It mutates the operand in place and requires the operand to be a variable, a property access, or an indexer access.

The operator operates in two distinct evaluation modes depending on its placement relative to the operand: prefix and postfix.

## Prefix Decrement (`--x`)

In prefix form, the operator decrements the operand's value and evaluates to the *new* mutated value. The decrement operation occurs before the value is yielded to the enclosing expression.

```csharp theme={"dark"}
int x = 5;
int y = --x; 
// Evaluation sequence:
// 1. x is decremented to 4.
// 2. The expression '--x' evaluates to 4.
// 3. y is assigned 4.
```

## Postfix Decrement (`x--`)

In postfix form, the operator evaluates to the *original* value of the operand, and then decrements the operand. The original value is temporarily cached for the enclosing expression while the underlying variable is mutated.

```csharp theme={"dark"}
int x = 5;
int y = x--; 
// Evaluation sequence:
// 1. The original value of x (5) is cached.
// 2. x is decremented to 4.
// 3. The expression 'x--' evaluates to the cached value (5).
// 4. y is assigned 5.
```

## Type Support and Semantics

The `--` operator is natively supported by the following types:

* **Integral types:** `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `nint`, `nuint`
* **Floating-point types:** `float`, `double` (subtracts `1.0` from the operand)
* **High-precision decimal:** `decimal`
* **Characters:** `char` (decrements the underlying UTF-16 code point)
* **Enumerations:** `enum` (decrements the underlying integral type)
* **Pointer types:** `T*` (in an `unsafe` context, subtracts `sizeof(T)` bytes from the memory address)

## Arithmetic Overflow

When applied to integral types, the behavior of the `--` operator upon decrementing past the minimum representable value of the type depends on the execution context:

* **`unchecked` context (default):** The operation silently underflows and wraps around to the maximum representable value of the type.
* **`checked` context:** The runtime throws a `System.OverflowException`.

```csharp theme={"dark"}
byte b1 = 0;
unchecked
{
    b1--; // b1 wraps around to 255
}

byte b2 = 0;
checked
{
    b2--; // Throws System.OverflowException
}
```

## Operator Overloading

Custom `class` and `struct` types can overload the `--` operator. When overloading, you only define the operator once; the C# compiler automatically handles the distinction between prefix and postfix evaluation based on the call site.

```csharp theme={"dark"}
public struct Vector1D
{
    public int X { get; set; }

    public static Vector1D operator --(Vector1D v)
    {
        return new Vector1D { X = v.X - 1 };
    }
}
```

## Execution Mechanics and Thread Safety

At the Intermediate Language (IL) level, the `--` operator is not a single atomic instruction. It expands into a read-modify-write sequence:

1. Load the value from memory.
2. Subtract 1 (or the appropriate offset).
3. Store the new value back to memory.

Because of this multi-step execution, the `--` operator is **not thread-safe**. If multiple threads concurrently apply the `--` operator to the same shared variable, race conditions will occur. For atomic decrements in multithreaded scenarios, the `System.Threading.Interlocked.Decrement` method must be used instead.

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