Thursday, 24 November 2016

c# - What is the difference between i++ and ++i?




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

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...