I’m having a hard time understanding what the difference is between incrementing a variable in C# this way:
myInt++;
and
++myInt;
When would ever matter which one you use?
I’ll give voteCount++ for the best answer. Or should I give it ++voteCount…
There is no difference when written on its own (as shown) – in both cases myInt will be incremented by 1.
But there is a difference when you use it in an expression, e.g. something like this:
In the first case, myInt is incremented and the new/incremented value is passed to MyFunction(). In the second case, the old value of myInt is passed to MyFunction() (but myInt is still incremented before the function is called).
Another example is this:
BTW: as Don pointed out in a comment the same rules are also valid for decrement operations, and the correct terminology for these operations are:
As Jon Skeet points out: