Why the increment of an integer on C# is executed after a function return its value?
Asked Answered
G

4

16

Why this two functions return different values?

When I call this function passing 0 as parameter it returns 1

public static int IncrementByOne(int number)
{
    return (number + 1);
}

However, when I call this function passing 0 as parameter it returns 0 even though the increment is executed and the number variable changes its value to 1 inside the method?

public static int IncrementByOne(int number)
{
    return number++;
}

What is the reason why the returned values of this two functions are different?

Grenadines answered 9/9, 2016 at 13:18 Comment(0)
S
30

number++ is a postincrement. It returns its current value before it is incremented. To get the same behaviour as in your first method, use a preincrement ++number

See documentation: https://msdn.microsoft.com/en-us/library/36x43w8w.aspx

Synn answered 9/9, 2016 at 13:20 Comment(0)
P
6

The value of the post-increment (postfix) ++ operator is the value of the operand before it is incremented. So if the current value is 2, the operator saves 2, increments it to 3 but returns the saved value.

For your function

public static int IncrementByOne(int number)
{
    return number++;
}

Look at the generated IL code to see what happens:

IncrementByOne:
    IL_0000:  ldarg.0        // load 'number' onto stack
    IL_0001:  dup            // copy number - this is the reason for the
                             // postfix ++ behavior
    IL_0002:  ldc.i4.1       // load '1' onto stack
    IL_0003:  add            // add the values on top of stack (number+1)
    IL_0004:  starg.s     00 // remove result from stack and put
                             // back into 'number'
    IL_0006:  ret            // return top of stack (which is
                             // original value of `number`)

The reason the postfix ++ operator returns the original (not the incremented) value is because of the dup statement - the value of number is on the stack twice and one of those copies stays on the stack by the ret statment at the end of the function so it gets returned. The result of the increment goes back into number.

Photomicrograph answered 9/9, 2016 at 13:21 Comment(6)
Downvoter, care to explain your vote? If you think something is wrong with this answer, let me know.Photomicrograph
The code and your description of it are opposites. You say that the value is returned before the increment, but read the code. The ret happens after the increment!Mayer
A correct statement would be "the value of the operation is the value of the operand before it is incremented". So the sequence goes: evaluation, increment, return. This is all clearly documented in the specification.Mayer
@EricLippert Ok, I may have mis-phrased it. I thought I wrote it out correctly. I'll update the answer.Photomicrograph
Would it had been easier to say that at the point the method returns, the value on the stack is the value of number before the increment (and therefore is the return value). The incremented value has been stored in the local but not pushed onto the stack.Stutman
@ScottPerham Perhaps it would've been easier but I didn't think of that. My original phrasing seemed clear to me - it was supposed to mean what you and Eric Lippert were suggesting but looking back, even I see that it was misleading at the least and perhaps downright wrong.Photomicrograph
C
2

Or, to point out the caveman approach...

public static int IncrementByOne(int number)
{
    number++;
    return number;
}
Carbuncle answered 9/9, 2016 at 15:33 Comment(0)
M
0

The last function post-increments number; If you want immediate incremented value, you can try return ++number;

Metrorrhagia answered 9/9, 2016 at 13:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.