t3rse
t3rse

Reputation: 10124

C# Compiler Behavior Question?

Hey everyone, in the following code, what should the result of d be after the second expression?

        int d = 1;
        d += d++;

One would assume d is 3 afterwards but the unary increment d++ doesn't seem to take effect and d retains a value of 2.

Is there a name for this bug? Does it exist for other compilers that support unary increment like C#?

Upvotes: 2

Views: 578

Answers (9)

LJM
LJM

Reputation: 6444

I think behavior of d++ is different than ++d, though the final result stored in d is the same.

Upvotes: 2

Brian Rasmussen
Brian Rasmussen

Reputation: 116471

If you take a look at the generated IL, you'll see why the result is 2 and not 3.

IL_0000:  ldc.i4.1  // load constant 1 on evaluation stack
IL_0001:  stloc.0   // pop and store value in local 0
IL_0002:  ldloc.0   // load value of local 0 on evaluation stack
IL_0003:  ldloc.0   // repeat, stack is now 1, 1
IL_0004:  dup       // duplicate topmost value on evaluation stack, 
                    // i.e. stack is now 1, 1, 1
IL_0005:  ldc.i4.1  // load constant 1 on evaluation stack
IL_0006:  add       // add two topmost values on stack, 
                    // i.e. 1 and 1 and push result on stack
IL_0007:  stloc.0   // pop and store this value in local 0
IL_0008:  add       // add the two remaining values on the stack
                    // which again happens to be 1 and 1 and push result to stack
IL_0009:  stloc.0   // pop and store this value in local 0

In other words: The final value stored is the sum of 1 and 1.

(the code above is from release mode build)

Upvotes: 16

Eric Lippert
Eric Lippert

Reputation: 660407

I frequently get questions about the ++ operators being "broken"; almost always it is because the person asking the question is used to the way it works in some language where behaviour ++ is not well-defined, like C++. Here's a recent article I wrote about such a scenario:

http://blogs.msdn.com/ericlippert/archive/2009/08/10/precedence-vs-order-redux.aspx

Upvotes: 4

Donut
Donut

Reputation: 112865

If you rewrite your code this way, it will set d to have a value of 3:

int d = 1;
d += ++d;

Take a look at the ++ Operator documentation for an explanation as to why your example behaves the way it does.
Excerpt:

The second form is a postfix increment operation. The result of the operation is the value of the operand before it has been incremented.

As @Guffa pointed out, it's not a bug, it's simply that the result of your postfix increment operation in d is being overwritten by the += operation.

Upvotes: 12

Guffa
Guffa

Reputation: 700650

It's not a bug, it acts exactly as expected.

The += operator expands into this:

d = d + d++;

That means that the change that the ++ operator causes is overwritten when the result is assigned back to the variable.

Upvotes: 33

tvanfosson
tvanfosson

Reputation: 532605

...and this would be an example of the reason why I find the post/pre- increment/decrement operators to be highly unreadable when used in an expression with other operators. The behavior you describe is correct, but hard to reason about, leading to misunderstandings and bugs.

Even though it's wordier I would rewrite this as:

int d = 1;
d += d;
++d;

Note the use of the pre-increment operator instead of the post-increment so that the compiler doesn't think it needs to keep a copy of the old value.

Upvotes: 1

JesperE
JesperE

Reputation: 64434

d++ and ++d are different. Also known as "Select Isn't Broken."

Upvotes: 1

Amy B
Amy B

Reputation: 110181

Silly code is unpredictable. Might I recommend

d += 2;

Upvotes: 1

wsorenson
wsorenson

Reputation: 5971

Have you tried ++d? Isn't d++ evaluated after?

Upvotes: 2

Related Questions