I\'m having a hard time understanding what the difference is between incrementing a variable in C# this way:
myInt++;
and
Others have shown where it makes a difference, and have commented that as a single statement it doesn't make a difference.
I'd like to add that it's almost always a bad idea to use it where it makes a difference. I suspect there may be some times where it's more readable to have code such as:
Console.WriteLine("Foo: {0}", foo++);
than:
Console.WriteLine("Foo: {0}", foo);
foo++;
... but they're very rare! The latter of these two samples makes the ordering crystal clear immediately - the former requires a bit of thinking (to my poor brain, anyway). Think of the readability first.
If for example you do something like this:
int myInt = 5;
Foo(++myInt);
void Foo(int x)
{
Console.WriteLine(x);
}
This will print out 6.
Foo(myInt++);
will print out 5;
Basically, ++myInt increments FIRST uses the variable SECOND. myInt++ is the opposite.
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:
MyFunction(++myInt);
MyFunction(myInt++);
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:
int myInt = 1;
int a = ++myInt;
// myInt is incremented by one and then assigned to a.
// Both myInt and a are now 2.
int b = myInt++;
// myInt is assigned to b and then incremented by one.
// b is now 2, myInt is now 3
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:
++i; // pre-increment
i++; // post-increment
--i; // pre-decrement
i--; // post-decrement
As Jon Skeet points out:
Others have shown where it makes a difference, and have commented that as a single statement it doesn't make a difference.
I'd like to add that it's almost always a bad idea to use it where it makes a difference. I suspect there may be some times where it's more readable to have code such as:
Console.WriteLine("Foo: {0}", foo++);
than:
Console.WriteLine("Foo: {0}", foo); foo++;
... but they're very rare! The latter of these two samples makes the ordering crystal clear immediately - the former requires a bit of thinking (to my poor brain, anyway). Think of the readability first.
It determines when the result for that operation is returned.
Here's an example from the MSDN site:
static void Main()
{
double x;
x = 1.5;
Console.WriteLine(++x);
x = 1.5;
Console.WriteLine(x++);
Console.WriteLine(x);
}
And the ouput:
2.5
1.5
2.5
It doesn't unless it is in an expression, i.e.
myInt=1;
x=myInt++;
is different to:
myInt=1;
x=++myInt;
The first assigns 1 to x, because the assignment happens before the increment.
The second assigns 2 to x, because the assignment happens after the increment.
In assignment or expressions, for example
x = ++myInt; // x = myInt + 1
x = myInt++; // x = myInt
This can also be used in expressions like for loops or if statements. For example take the following where seek is 0 and count is 3.
while(seek++ < count) {
Console.WriteLine(seek);
}
results in outputs of 1, 2 and 3, for seek, and the following
while(++seek < count) {
Console.WriteLine(seek);
}
Results in 1 and 2 for seek. So ++myInt increments myInt before its value is evaluated, where as myInt++ is incremented after it is evaluated.