Possible Duplicate:
C#: what is the difference between i++ and ++i?
I see this operator (++) very often. I know what it does ultimately, but it seems like there’s some rules I don’t understand. For example, it seems to matter if you put it before or after the variable you’re using it on. Can someone explain this?
The statement
is exactly equivalent to
except that
xis evaluated only once (which makes a difference if it is an expression involving property getters).The difference between the following two:
Is that in the first one, the method
DoSomethingwill see the previous value ofxbefore it was incremented. In the second one, it will see the new (incremented) value.For more information, see C# Operators on MSDN.
It is possible to declare a custom
++operator for your own classes, in which case the operator can do something different. If you want to define your own++operator, see Operator Overloading Tutorial on MSDN.