TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
-= 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:
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 asint, float, double, decimal) or enum types, the operator subtracts the right operand from the left operand.
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?).
null during a numeric subtraction, the underlying subtraction yields null. The -= operator then assigns null back to the left-hand operand.
Pointer Arithmetic
In anunsafe context, the -= operator can be applied to a pointer type and an integer type to perform pointer arithmetic.
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.
- 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
removeaccessor (e.g.,remove_MyEvent(rightOperand)), which encapsulates the actual thread-safe or custom removal logic defined by the event. - Invocation List Modification: The
Delegate.Removeoperation 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 tonulland assignsnullback to the left operand. This is an observable assignment; if the left operand is a property, its setter will be invoked with anullvalue. - 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
nulland assignsnullto the left operand.
- If the left operand is
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.
Master C# with Deep Grasping Methodology!Learn More





