Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 246
Esteban Verbel Avatar asked Sep 09 '16 13:09

Esteban Verbel


People also ask

Why is i ++ used in C?

In C, ++ and -- operators are called increment and decrement operators. They are unary operators needing only one operand. Hence ++ as well as -- operator can appear before or after the operand with same effect. That means both i++ and ++i will be equivalent.

What is the purpose of ++ operator?

Description. If used postfix, with operator after operand (for example, x++ ), the increment operator increments and returns the value before incrementing. If used prefix, with operator before operand (for example, ++x ), the increment operator increments and returns the value after incrementing.

What is increment operators in C?

The decrement (–) and increment (++) operators are special types of operators used in programming languages to decrement and increment the value of the given variable by 1 (one), respectively.

Why do we use ++ A and A ++ as different?

++a returns the value of an after it has been incremented. It is a pre-increment operator since ++ comes before the operand. a++ returns the value of a before incrementing. It is a post-increment operator since ++ comes after the operand.


2 Answers

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

like image 139
Flat Eric Avatar answered Nov 14 '22 14:11

Flat Eric


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.

like image 27
xxbbcc Avatar answered Nov 14 '22 14:11

xxbbcc