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.
++ (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.
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.
Underlying Mechanics
For an operandx 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.
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*inunsafecontexts (increments the memory address bysizeof(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.
Thread Safety and Atomicity
The++ operator is not atomic. It executes a non-indivisible read-modify-write sequence:
- Read the current value from memory into a CPU register.
- Increment the value in the register.
- Write the new value back to memory.
++ 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.
Master C# with Deep Grasping Methodology!Learn More





