> ## 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# Pre-Increment

The `++` (increment) operator is a unary arithmetic operator that increments the value of its operand by 1. It mutates the operand in place and requires the operand to be a variable, a property access, or an indexer access. It cannot be applied to literals or constants.

The operator manifests in two distinct syntactical forms, which dictate the order of evaluation relative to the expression's return value.

## Prefix Increment (`++x`)

The prefix form increments the operand's value and evaluates to the **newly incremented** value. The mutation occurs before the value is yielded to the surrounding expression.

```csharp theme={"dark"}
int x = 10;
int y = ++x; 

// Evaluation sequence:
// 1. x is incremented to 11
// 2. The expression ++x evaluates to 11
// 3. y is assigned 11
```

## Postfix Increment (`x++`)

The postfix form evaluates to the **original** value of the operand, but the mutation of the operand occurs *during* the evaluation of the postfix expression itself, before the surrounding expression continues evaluating.

Under the hood, the C# compiler reads the original value, saves it to a temporary location, increments the operand, and then returns the saved temporary value as the result of the expression.

```csharp theme={"dark"}
int a = 10;
int b = a++; 

// Evaluation sequence:
// 1. The original value of a (10) is temporarily saved
// 2. a is incremented to 11
// 3. The expression a++ evaluates to the saved value (10)
// 4. b is assigned 10
```

Because the mutation happens immediately upon evaluating the postfix expression, subsequent reads of the operand within the same surrounding expression will observe the incremented value:

```csharp theme={"dark"}
int x = 1;
int y = x++ + x; 

// Evaluation sequence (left-to-right):
// 1. x++ evaluates to the saved original value (1), and x is mutated to 2
// 2. The second operand x is evaluated, yielding its new value (2)
// 3. The addition is performed: 1 + 2
// 4. y is assigned 3
```

## Underlying Mechanics

For an operand `x` of type `T`, the increment operation is conceptually evaluated as `x = (T)(x + 1)`. However, the C# compiler ensures that the operand `x` is evaluated only once.

When applied to integral types smaller than `int` (such as `byte` or `short`), the operation does not require an explicit cast back to the original type, despite the underlying addition being performed using 32-bit integers.

```csharp theme={"dark"}
byte b = 255;
b++; // Silently overflows to 0 in an unchecked context
```

## Supported Types

The `++` operator is natively supported by:

* **Integral types:** `sbyte`, `byte`, `short`, `ushort`, `int`, `uint`, `long`, `ulong`, `char`
* **Floating-point types:** `float`, `double`
* **High-precision decimal:** `decimal`
* **Enumerations:** `enum` (increments the underlying integral value)
* **Pointers:** `T*` in `unsafe` contexts (increments the memory address by `sizeof(T)`)

## Operator Overloading

User-defined types (`class` or `struct`) can overload the `++` operator. When overloading, you only define a single `operator ++` method. The C# compiler automatically handles the distinct evaluation logic for both prefix and postfix usage based on this single overload.

```csharp theme={"dark"}
public struct Vector
{
    public int X;
    
    // The overload must be static and return the containing type
    public static Vector operator ++(Vector v)
    {
        v.X++;
        return v;
    }
}
```

## Thread Safety and Atomicity

The `++` operator is **not atomic**. It executes a non-indivisible read-modify-write sequence:

1. Read the current value from memory into a CPU register.
2. Increment the value in the register.
3. Write the new value back to memory.

Because of this, applying `++` to a shared variable in a multithreaded environment will result in race conditions and lost updates. For atomic increments, C# provides the `System.Threading.Interlocked.Increment` method.

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