C# Reference ยท Operators and Expressions
C# increment and decrement operators
Reference for the C# increment (++) and decrement (--) operators: prefix versus postfix, the value of the expression, properties, overflow and overloading.
++ adds 1 to a variable and -- subtracts 1. Each has two forms. The postfix form (i++) produces the old value and then updates the variable. The prefix form (++i) updates first and produces the new value.
The operand must be a variable, a property, an indexer or another assignable location, not a literal or an expression result. When the result is not used, as in a for loop header, both forms do the same thing.
int i = 5; int a = i++; // a = 5, i = 6 int b = ++i; // i = 7, b = 7
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
x++; // postfix increment x--; // postfix decrement ++x; // prefix increment --x; // prefix decrement public static T operator ++(T value); // user-defined public static T operator --(T value);
| Form | Value of the expression | Effect on the variable |
|---|---|---|
x++ |
the value before the change | x is increased by 1 |
++x |
the value after the change | x is increased by 1 |
x-- |
the value before the change | x is decreased by 1 |
--x |
the value after the change | x is decreased by 1 |
Returns: a value of the operand's type.
Errors: in a checked context, going past the range of an integer type throws OverflowException; in the default unchecked context it wraps around. The operand cannot be a readonly field outside a constructor, a literal, or a method call result.
Usage notes
- Works on all integer types,
char, floating-point types,decimal, enums, pointers (inunsafecode), and any type that defines the operator. - Prefer the form you do not have to think about. As a standalone statement (
i++;) the two forms are identical. Only use the value of the expression when it makes the code shorter and still obvious. - Do not modify a variable twice in one expression (
i = i++ + ++i). C# evaluates operands left to right so the result is defined, but readers will not trust it. - Properties and indexers are read once, changed and written once, so
obj.Count++runs the getter and the setter. With a computed property that has side effects, this can matter. - Thread safety:
x++on a shared field is not atomic. UseInterlocked.Increment(ref x)when several threads update it. - User-defined
++and--must bepublic staticand return the declaring type. The compiler assigns the returned value back to the variable, so value-like types should return a new instance rather than mutating. Since C# 14 you can also declare instance operators such aspublic void operator ++()that mutate the object in place, and checked variants (operator checked ++) since C# 11. - A common bug is writing
i = i++, which leavesiunchanged because the old value is assigned back after the increment. - Performance: equivalent to
x += 1; the JIT generates the same code.