Prefix vs. Postfix Increments
-
I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:
int counter = 0;
foreach (var thing in things) {
thingThatCares.TellMeWhereIAm(++counter);
}I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?
-
I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:
int counter = 0;
foreach (var thing in things) {
thingThatCares.TellMeWhereIAm(++counter);
}I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?
-
I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:
int counter = 0;
foreach (var thing in things) {
thingThatCares.TellMeWhereIAm(++counter);
}I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?
-
I've noticed I've been using a lot of prefix incrementors in code lately (esp. for counters), simple e.g.:
int counter = 0;
foreach (var thing in things) {
thingThatCares.TellMeWhereIAm(++counter);
}I typically don't see a lot of prefix operands in other folks' code, so I'm wondering if there's a better way of doing things like that, or it's just stylistic, etc.?
There should be no difference except that one happens after the operation, the other before. However, in a for loop it does not matter:
for (int i = 0; i < 3; ++i)
Console.Write(i);for (int i = 0; i < 3; i++)
Console.Write(i);Yields the same result. However these do not:
for (int i = 0; i < 3;)
Console.WriteLine(i++);for (int i = 0; i < 3;)
Console.WriteLine(++i);