What the operators do
Adding 1 to a variable or taking 1 away is so common that C# has a short form for each. The increment operator ++ adds 1, and the decrement operator -- subtracts 1:
int number = 10;
number++; // same as number += 1
Console.WriteLine(number); // 11
number--; // same as number -= 1
Console.WriteLine(number); // 10
Both operators change the variable itself, so the variable must be a real, assignable variable. That means it can't be a const, and it can't be a literal: 5++ is a compile error. They work on the other numeric types too. A double goes up by 1.0, and a char moves to the next character, so 'a' becomes 'b'.
Postfix and prefix forms
Each operator can go after the variable (number++, postfix) or before it (++number, prefix). On a line of their own, the two forms do exactly the same thing. The difference appears only when you use the expression's result, for example by assigning it to another variable.
- Postfix (
number++) gives you the value from before the change, then changes the variable. - Prefix (
++number) changes the variable first, then gives you the value after the change.
int number = 10;
int oldNumber = number++;
Console.WriteLine(oldNumber); // 10
Console.WriteLine(number); // 11
int other = 10;
int newNumber = ++other;
Console.WriteLine(newNumber); // 11
Console.WriteLine(other); // 11
The decrement operator follows the same rule. int old = n--; with n equal to 10 leaves old as 10 and n as 9, while int q = --n; would give q the new value 9.
A handy way to remember it: the operator's position tells you when the change is visible to the rest of the expression. If the operator comes first, you see the change straight away. If the name comes first, you see the old value.
Most of the time you'll use them as counters: a variable that goes up by one each time something happens, such as a score or the number of items seen so far, or down by one as something is used up. In that role the prefix and postfix forms are interchangeable, and many programmers default to the postfix one.
Working through the exercise
The exercise builds one variable up step by step and records what each form returns. Follow the value of number as you go:
- Create
int number = 18;. It has to be an ordinary variable, because the operators modify it. - Write
number++;on its own line. Nothing uses the result, sonumberbecomes 19. - Write
int post = number++;. The postfix form hands back the old value, sopostis 19, and thennumberbecomes 20. - Write
int pre = ++number;. The prefix form increments first, sonumberbecomes 21 andpreis 21. - Write
--number;to take 1 away. Nownumberis 20, whilepoststays 19 andprestays 21.
The finished code is short:
int number = 18;
number++;
int post = number++;
int pre = ++number;
--number;
Notice that post and pre are ordinary copies. They hold plain numbers, and later changes to number don't affect them.
Gotchas
Assigning a variable its own postfix result
This line looks like it should add 1, but it doesn't:
int x = 5;
x = x++;
Console.WriteLine(x); // 5
x++ produces the old value 5 and bumps x to 6, and then the assignment writes the old value 5 back into x. If you just want to add 1, write x++; by itself.
Using the variable twice in one expression
C# evaluates an expression from left to right, so the change is already visible to whatever comes after it:
int a = 5;
int b = a++ + a;
Console.WriteLine(b); // 5 + 6 = 11
Code like this is correct but hard to read. Prefer a separate a++; statement when you can.
Array indexes
The postfix form is common when stepping through an array, because it uses the current index and then moves on:
int[] values = { 10, 20, 30 };
int i = 0;
Console.WriteLine(values[i++]); // 10
Console.WriteLine(values[i++]); // 20
Console.WriteLine(i); // 2
Overflow
An int has a largest value, int.MaxValue. By default C# doesn't complain when you pass it: int m = int.MaxValue; m++; silently wraps around to -2147483648.
Key takeaways
++adds 1 and--subtracts 1, changing the variable in place.- On a line of their own,
x++and++xare identical. - Postfix returns the old value; prefix returns the new value.
- Don't write
x = x++; it leavesxunchanged. - Keep the operators in simple statements, and the order of evaluation never becomes a puzzle.