Postfix and Prefix increment and decrement
suggest changePostfix increment X++ will add 1 to x
var x = 42;
x++;
Console.WriteLine(x); // 43
Postfix decrement X-- will subtract one
var x = 42;
x--;
Console.WriteLine(x); // 41
++x is called prefix increment. It increments the value of x and then returns x, while x++ returns the value of x and then increments it.
var x = 42;
Console.WriteLine(++x); // 43
Console.WriteLine(x); // 43
while
var x = 42;
Console.WriteLine(x++); // 42
Console.WriteLine(x); // 43
Both are commonly used in a for loop.
for(int i = 0; i < 10; i++)
{
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents