Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
C# provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable. These are the unary increment operator, ++, and the unary decrement operator, --, respectively, which are summarized in Fig. 5.14. An application can increment by 1 the value of a variable called c using the increment operator, ++, rather than the expression c = c + 1 or c += 1. An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the prefix increment operator or prefix decrement operator, respectively. An increment or decrement operator that is postfixed to (placed after) a variable is referred to as the postfix increment operator or postfix decrement operator, respectively.
| Operator | Called | Sample expression | Explanation |
|---|---|---|---|
| ++ | prefix increment | ++a | Increments a by 1, then uses the new value of a in the expression in which a resides. |
| ++ | postfix increment | a++ | Uses the current value of a in the expression in which a resides, then increments a by 1. |
| -- | prefix decrement | --b | Decrements b by 1, then uses the new value of b in the expression in which b resides. |
| -- | postfix decrement | b-- | Uses the current value of b in the expression in which b resides, then decrements b by 1. |