> ## 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# Subtraction Assignment

The `-=` operator is a compound assignment operator in C# that performs arithmetic subtraction, pointer decrementing, or delegate removal on its left-hand operand using the value of its right-hand operand, subsequently assigning the result back to the left-hand operand.

At compile time, an expression using the `-=` operator is generally evaluated as:

```csharp theme={"dark"}
x = (T)(x - y);
```

*(Where `T` is the type of `x`)*

Unlike the expanded `x = x - y` form, the left-hand operand in a `-=` operation is evaluated exactly once. For example, in the expression `array[GetIndex()] -= 5;`, the method `GetIndex()` is invoked only one time.

The operator functions across several distinct contexts depending on the types of the operands:

## Numeric and Enumeration Subtraction

When applied to numeric types (such as `int`, `float`, `double`, `decimal`) or `enum` types, the operator subtracts the right operand from the left operand.

```csharp theme={"dark"}
x -= y;
```

If the right operand is of a narrower type than the left operand, it is implicitly converted. The final result is explicitly cast back to the type of the left operand if the underlying binary subtraction naturally promotes the type (e.g., subtracting a `byte` from a `byte` results in an `int`, which the `-=` operator automatically casts back to a `byte` before assignment).

## Lifted Operators (Nullable Value Types)

The `-=` operator supports lifted operators for nullable value types (e.g., `int?`, `double?`).

```csharp theme={"dark"}
int? a = 10;
int? b = null;
a -= b; 
```

If either the left or right operand evaluates to `null` during a numeric subtraction, the underlying subtraction yields `null`. The `-=` operator then assigns `null` back to the left-hand operand.

## Pointer Arithmetic

In an `unsafe` context, the `-=` operator can be applied to a pointer type and an integer type to perform pointer arithmetic.

```csharp theme={"dark"}
unsafe 
{
    int* p = &someVariable;
    p -= 1;
}
```

Subtracting an integer `n` from a pointer `p` of type `T*` decrements the memory address held by the pointer by `n * sizeof(T)`.

## Delegate and Event Unsubscription

When applied to delegates or events, the `-=` operator removes a method reference from a multicast delegate's invocation list.

```csharp theme={"dark"}
MyDelegate -= DelegateMethod;
MyEvent -= EventHandlerMethod;
```

**Technical Characteristics:**

* **Underlying Mechanism (Delegates):** For raw delegate variables, the compiler translates this operation into a call to `System.Delegate.Remove(leftOperand, rightOperand)`.
* **Underlying Mechanism (Events):** For events, the compiler translates the operation into a call to the event's `remove` accessor (e.g., `remove_MyEvent(rightOperand)`), which encapsulates the actual thread-safe or custom removal logic defined by the event.
* **Invocation List Modification:** The `Delegate.Remove` operation searches the invocation list of the left operand for the *last* occurrence of a delegate that matches the right operand. If a match is found, it returns a new multicast delegate with that specific invocation removed.
* **Null Handling:**
  * If the left operand is `null`, the operation evaluates to `null` and assigns `null` back to the left operand. This is an observable assignment; if the left operand is a property, its setter will be invoked with a `null` value.
  * If the right operand is not found in the left operand's invocation list, the left operand's value remains unchanged.
  * If the removal results in an empty invocation list, the operation evaluates to `null` and assigns `null` to the left operand.

## Operator Overloading

The `-=` operator cannot be explicitly overloaded in C#. However, if a user-defined `struct` or `class` overloads the binary subtraction operator (`-`), the `-=` operator is implicitly supported and will utilize the custom subtraction logic before performing the assignment.

```csharp theme={"dark"}
public static Vector2 operator -(Vector2 a, Vector2 b) 
{
    return new Vector2(a.X - b.X, a.Y - b.Y);
}

// The following is now implicitly supported:
// vectorA -= vectorB;
```

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