the return value of level++
will be level
and therefore
pass level
into DoStuff
. This could be a fairly nasty bug as the recursion will never end (from what is shown DoStuff
is always being passed with the same value). Perhaps ++level
or level + 1
is meant instead?
level + 1
will pass level + 1
into DoStuff
and leave level
unchanged for the rest of the function.
The post-increment operator (variable++) is precisely equivalent to the function
int post_increment(ref int value)
{
int temp = value;
value = value + 1
return temp;
}
while the pre-increment operator (++variable) is precisely equivalent to the function
int pre_increment(ref int value)
{
value = value + 1;
return value;
}
Therefore, if you expand the operator inline into the code, the operators are equivalent to:
DoStuff(a + 1)
int temp = a + 1;
DoStuff(temp);
DoStuff(++a)
a = a + 1;
DoStuff(a);
DoStuff(a++);
int temp = a;
a = a + 1;
DoStuff(temp);
It is important to note that post-increment is not equivalent to:
DoStuff(a);
a = a + 1;
Additionally, as a point of style, one shouldn't increment a value unless the intention is to use the incremented value (a specific version of the rule, "don't assign a value to a variable unless you plan on using that value"). If the value i + 1
is never used again, then the preferred usage should be DoStuff(i + 1)
and not DoStuff(++i)
.