A classmate of mine asked this question, and I wanted to be sure that I was telling him the correct answer. Essentially the code below (which does indeed display "6" as the answer) confused him when it got to MessageBox.Show((i--).ToString());.
My explanation was that the decrement operation (i--) isn't actually taking place, since it is being passed to the .Show method of the MessageBox object. So it displays 6 because it's not actually reducing that value by 1.
Is this the correct explanation? I've never tried to throw inc/dec operations while also displaying values at the same time, so I'm not sure if my reasoning on this is correct. Thanks!
int i = 6; // I starts as 6...
if (i >=4 ) // Then the check to see if i >= 4 is TRUE...
{
if( i == 5) // The check to see if i == 5 is FALSE...
{
MessageBox.Show(i.ToString());
}
else
{
MessageBox.Show((i--).ToString()); // ****
}
}
else
{
MessageBox.Show((i++).ToString());
}