Team LiB
Previous Section Next Section

Pre/Post Operators

The increment (++) and decrement (--) operators have two usages. That is, they can be used for both pre-operation and post-operation evaluation. When the operators appear before the variable, the operation takes place before the variable is evaluated. When they follow the variable, the operation takes place after the variable has been evaluated. Take a look at the following statements:

int i = 10;
Console.WriteLine( i++ ); //prints 10, then increments i to 11
Console.WriteLine( ++i ); //increments i to 12 and prints 12

When the value of i is sent to the WriteLine method the first time, i is evaluated to 10 and that value is passed to the WriteLine method. The variable i is then incremented to 11. On the next line, because the increment operator is before the variable (and therefore a pre-operation operator), i is incremented before its value is evaluated and passed to the WriteLine method.

    Team LiB
    Previous Section Next Section