What is the difference between i++ and ++i?

后端 未结 6 2061
暗喜
暗喜 2020-11-21 23:13

I\'ve seen them both being used in numerous pieces of C# code, and I\'d like to know when to use i++ or ++i (i being a number variable

6条回答
  •  面向向阳花
    2020-11-21 23:36

    Oddly it looks like the other two answers don't spell it out, and it's definitely worth saying:


    i++ means 'tell me the value of i, then increment'

    ++i means 'increment i, then tell me the value'


    They are Pre-increment, post-increment operators. In both cases the variable is incremented, but if you were to take the value of both expressions in exactly the same cases, the result will differ.

提交回复
热议问题