A common requirement is to add a value to a variable, subtract a value from a variable, or otherwise change the mathematical value, and then to assign that new value back to the original variable. C# provides several operators for these calculations.
Suppose you want to increment the mySalary
variable by 5,000. You can do this
by writing:
mySalary = mySalary + 5000;
In simple arithmetic, this would make no sense, but in C#, this line means, “Add 5,000 to the value in mySalary, and assign the sum back to mySalary.” Thus, after this operation completes, mySalary will have been incremented by 5,000. You can perform this kind of assignment with any mathematical operator:
mySalary = mySalary * 5000; mySalary = mySalary - 5000;
and so forth.
The need to perform this kind of manipulation is so common that
C# includes special operators for self-assignment. Among these
operators are +=
, -=
, *=
,
/=
, and %=
, which, respectively, combine addition,
subtraction, multiplication, division, and modulus, with
self-assignment. Thus, you can write the previous examples as:
mySalary += 5000; mySalary *= 5000; mySalary -= 5000;
These three instructions, respectively, increment mySalary by 5,000, multiply mySalary by 5,000, and subtract 5,000 from the mySalary variable.
Because incrementing and decrementing by exactly 1 is a
very common need, C# provides two additional special operators for
these purposes: increment (++
) and
decrement (—).
Thus, if you want to increment the variable myAge
by 1, you can write:
myAge++;
This is equivalent to writing:
myAge += 1;
To complicate matters further, you might want to increment a variable and assign the results to a second variable:
resultingValue = originalValue++;
The question arises: do you want to assign before you increment
the value or after? In other words, if originalValue
starts out with the value 10,
do you want to end with both resultingValue
and originalValue
equal to 11, or do you want
resultingValue
to be equal to 10
(the original value) and originalValue
to be equal to 11?
C# offers two specialized ways to use the increment and
decrement operators: prefix and postfix . The way you use the ++
operator determines the order in which
the increment/decrement and assignment take place. The semantics of
the prefix increment operator is “increment the original value and
then assign the incremented value to result” while the semantics of
the postfix increment operator is “assign the original value to
result, and then increment original.”
To use the prefix operator to increment, place the ++
symbol before the variable name; to use
the postfix operator to increment, place the ++
symbol after the variable name:
result =++
original; // prefix result = original++
; // postfix
It is important to understand the different effects of prefix and postfix, as illustrated in Example 4-3. Note the output.
Example 4-3. Prefix and postfix operators
using System; class Values { static void Main( ) { int original = 10; int result; // increment then assign result = ++original; Console.WriteLine( "After prefix: {0}, {1}", original, result ); // assign then increment result = original++; Console.WriteLine( "After postfix: {0}, {1}", original, result ); } }
The output looks like this:
After prefix: 11, 11 After postfix: 12, 11
The prefix and postfix operators can be applied, with the same logic, to the decrement operators, as shown in Example 4-4. Again, note the output.
Example 4-4. Decrementing prefix and postfix
using System; class Values { static void Main( ) { int original = 10; int result; // increment then assign result = --original; Console.WriteLine( "After prefix: {0}, {1}", original, result ); // assign then increment result = original--; Console.WriteLine( "After postfix: {0}, {1}", original, result ); } }
The output looks like this:
After prefix: 9, 9 After postfix: 8, 9