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 like int
, float
, double
, etc). Anyone who knows this?
Answer
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.
No comments:
Post a Comment