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 unary arithmetic operator that decrements the value of its operand by 1. It mutates the operand in place and is supported by all built-in integral and floating-point numeric types, decimal, char, enumeration types, and pointer types (T*).
The operator exists in two distinct syntactical forms, which dictate the order of evaluation relative to the assignment or expression execution:
Prefix Decrement (--x)
The operand is decremented first, and the expression evaluates to the new decremented value.
Postfix Decrement (x--)
The expression evaluates to the original value of the operand, and the operand is decremented immediately afterward.
Execution Mechanics
Under the hood, the-- operator performs a read-modify-write operation. The compiler translates the operation into the following sequence:
- Read the current value of the operand.
- Subtract
1(or1.0for floating-point types) from the value. - Write the mutated value back to the operand.
get accessor to retrieve the value, performs the subtraction, and then emits a call to the set accessor to assign the decremented value.
Type-Specific Behaviors and Overflow
- Integral Types: Decrementing the minimum representable value of an integral type depends on the arithmetic context. In a
checkedcontext, the operation throws aSystem.OverflowException. In anuncheckedcontext, the value wraps around to the maximum representable value for that type. - Pointer Types (
T*): Inunsafecontexts, the--operator performs pointer arithmetic. It decrements the memory address held by the pointer bysizeof(T)bytes, rather than strictly subtracting 1. - Characters and Enumerations: For
chartypes, the operator decrements the underlying 16-bit unsigned integer (UTF-16 code unit). Forenumtypes, it decrements the underlying integral type of the enumeration.
Thread Safety
Because the-- operator executes as a multi-step read-modify-write sequence, it is not atomic and therefore not thread-safe. If multiple threads apply the -- operator to the same variable concurrently, race conditions will occur, leading to lost updates.
To perform an atomic decrement operation in a multithreaded context, you must bypass the -- operator and use the Interlocked class:
Operator Overloading
User-defined types (class and struct) can overload the -- operator to define custom decrement behavior. When you overload the -- operator, you only define a single method. The C# compiler automatically derives both prefix and postfix semantics from this single implementation.
vector-- or --vector, it invokes the overloaded method, manages the temporary storage of the original value for postfix operations, and assigns the returned instance back to the operand.
Master C# with Deep Grasping Methodology!Learn More





