/ decrement

What's the Difference Between i++ and ++i in C#?

There’s common confusion between the difference of the Prefix (++i) and Postfix (i++) increment operators in C#. A common explanation would be that “The Prefix operator increases iprior to running the statement and the Postfix operator increases iafter running the statement.” Although this is the perceived functionality, it’s actually false. The order of events in time is the same for both statements – it’s the return value that’s different. Eric Lippert explains it very well in this Stack Overflow post:

First, the order of events in time is exactly the same in both cases. Again, it is absolutely not the case that the order of events in time changes between prefix and postfix. It is entirely false to say that the evaluation happens before other evaluations or after other evaluations. The evaluations happen in exactly the same order in both cases.

The difference between the forms is what is returned. The Postfix form returns the original value, whereas the Prefix form increments the value and returns the result of the increment. The MSDN article on the Postfix increment and decrement operators explains the exact order of operations on a variable x:

  • x is evaluated to produce the variable
  • The value of x is saved
  • The selected operator is invoked with the saved value of x as its argument.
  • The value returned by the operator is stored in the location given by the evaluation of x.
  • The saved value of x becomes the result of the operation.

Compare that to the steps of the Prefix operator:

  • x is evaluated to produce the variable.
  • The selected operator is invoked with the value of x as its argument.
  • The value returned by the operator is stored in the location given by the evaluation of x.
  • The value returned by the operator becomes the result of the operation.

The Postfix form saves the original value, increments the value and assigns it to x, but then returns the original saved value. The Prefix form returns the result of the increment. Easy enough, right?

Now let’s have some fun. What happens when we run an operation such as x = x++ + ++x;? Let’s find out. Here’s a sample program:

static void Main()
{
    int x = 0;
    x = x++ + ++x;
    Console.WriteLine(x);
    Console.Read();
}

The result of the operation is 2. Why is that? Why is it not 3? Or 1? Well, using our newfound knowledge of the Prefix and Postfix operators let’s run through the x = x++ + ++x line.

OperationResult
The Postfix `x++` operation runs, incrementing `x` but returning the original value. 0
The Prefix `++x` operation runs, incrementing `x` (which is now 1 due to the Postfix operation!) and returns the new value. 2
The addition operator runs, performing `0 + 2`. 2

(Note that the Postfix operator has higher precedence than the Prefix operator. Eric Lippert, again, explains it well here.)

Make sense? Remember – it’s absolutely not the case that the operations run before or after other evaluations. The C# specification defines the sequence of events in time in incredible detail and these operators are no exception.

comments powered by Disqus
David Zych

David Zych

Dave is a dad, husband, programmer, (amateur) photographer, half-Canadian, alumnus of CSUCI, and an overall nice guy.

Read More