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 -= 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:
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.
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?).
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.
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.
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.
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;
Master C# with Deep Grasping Methodology!Learn More