2

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());
        }
Security Hound
  • 2,577
  • 3
  • 25
  • 42
SalarianEngineer
  • 624
  • 4
  • 10
  • 20

4 Answers4

8

i-- returns the value of i and returns the value before the decrement.

You should read it logically in the lexical order (as was intended by the creators) :

puts the value
and then decrements

If it's 6 before, it returns 6 (which is printed) even if just after the value of i is 5.

Note that --i decrements and returns the decremented value.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • 2
    "returns the value of i and then decrements." this is wrong. The sequence of operations in both cases are **exactly** the same. The difference is which value actually *returned* (incremented or not) – Tigran May 03 '12 at 17:46
  • @Tigran It depends of what you mean by returning. This comment doesn't help make the problem clear as I was trying to give the logical meaning of the operator ^^ – Denys Séguret May 03 '12 at 17:47
  • 1
    @dystroy: look on [Eric Lippert answer](http://stackoverflow.com/questions/3346450/what-is-the-difference-between-i-and-i) – Tigran May 03 '12 at 17:51
  • As for every operation, it's important, when you go deeper, to know how it's implemented (at least know that it's not atomic and not "thread-safe"). But this kind of deep detail is interesting when you're talking about the details of the operator, not when analyzing the upper level algorithm. I agree your link is very interesting. It's just off topic. – Denys Séguret May 03 '12 at 17:55
  • @Tigran that link was extremely useful and interesting. I didn't realize things went that deep...very cool! Thank you, everyone, for answering the question! – SalarianEngineer May 03 '12 at 18:25
4

i-- returns the pre-decrement value. If you want the value after decrementing, you want --i

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
2

i-- means decrement i after using it.

Eric H
  • 1,759
  • 1
  • 11
  • 14
1

This (variable--) is a postfix decrement operation.

From msdn:

The result of the operation is the value of the operand "before" it has been decremented.

So ToString() is applied on the present value(6) of 'i', then the it's value is decremented.

Also see C# spec. in msdn: Postfix increment and decrement operators

Kibria
  • 1,865
  • 1
  • 15
  • 17