2

I recall reading somewhere that for a variable int i both ++i and i++ generate a temporary in C#. Does anyone know why that is the case? Is their performance identical?

UPDATE

So from Eric Lippert's answer in the duplicate, the steps for pre-increment are as follows:

For pre-increment

1) x is evaluated to produce the variable

2) the value of the variable is copied to a temporary location

3) the temporary value is incremented to produce a new value (not overwriting the temporary!)

4) the new value is stored in the variable

5) the result of the operation is the new value

Why is step 2 necessary? Why not increment the variable in place?

NeddySpaghetti
  • 13,187
  • 5
  • 32
  • 61
  • I guess they are the same. – Felice Pollano May 26 '14 at 09:53
  • ++i shouldn't create a temp. where did you get that info from? – Fedor Hajdu May 26 '14 at 09:54
  • @Fedor, Eric Lippert says so in his answer to the duplicate. – Frédéric Hamidi May 26 '14 at 09:55
  • 1
    It creates temp, [eric explains it here](http://stackoverflow.com/a/3346729/2530848) – Sriram Sakthivel May 26 '14 at 09:55
  • Never heard of `++i`? You normally see the variable naming in for-loops, when I is a synonym for iterate. But you can always name it the way you want... – Max May 26 '14 at 09:55
  • If you overload the `++` operator - you only return the incrementation of the current value. You never change the current value. So - the code that calls to the `++` operator generates a temporary. Then, depending on the fix (prefix or postfix) of the operator, the temporary would returned as expression, and assigned to the variable, or just assigned to that variable (and its original value would be returned). – Shlomi Borovitz May 26 '14 at 10:01
  • I updated the question, was just wandering why the variable isn't incremented in place. – NeddySpaghetti May 26 '14 at 10:13
  • 1
    @Ned, I believe Eric explains it: `x could be volatile and changing on another thread; the value returned by x++ is not the current value of x, it is what x was assigned, and that could be different`. In the preincrement case, the same principle applies (variable could be volatile or another thread may be changing it). – Frédéric Hamidi May 26 '14 at 10:21

1 Answers1

1

The answer is: they don't. Not in the sense implied by the title.

First please note: as per Eric's article (which, surprisingly, was criticised by many), the sequence of events that takes place in pre-increment and post-increment in C# is identical. Not just similar, but identical. The same things happen in the same order.

Second, there are no 'temporaries' as such. the 'temporary location' Eric is referring to is conceptual, but corresponds in practice to a machine register. A value is copied from memory to a register, the value is incremented, and the value is stored back into memory.

But there is another aspect. Unlike C/C++, C# guarantees the order of evaluation of expressions. In C# the following code is well-defined, where in C it is not.

j = ++i + i++;

But take something simpler:

j = ++i + f(i); // first
j = i++ + f(i); // second

In the first case the value of the variable is available for use in the subsequent expression, but in the second case it is not. Instead there is a conceptual temporary resulting from the first increment that is used in the following function call. An additional register may be used in the second case.

david.pfx
  • 10,520
  • 3
  • 30
  • 63