
Int values are incremented in several ways. Some options include the pre-increment and post-increment operators. Although incrementing integers is trivial in programming languages, there are subtle issues related to the exact order of evaluation. These issues derive from load and store operations in register machines.
This C# article shows how increment works. It covers performance and syntax.

Here we increment int variables in different ways using the C# programming language. The program text shows both the ++ operator (with two pluses) and the += operator (with a plus and an equals). Additionally, it demonstrates how putting the ++ operator before or after the identifier affects the order of evaluation, sometimes in an important way.
Program that increments integers [C#]
using System;
class Program
{
static void Main()
{
//
// Shows how increments work.
//
int i = 0;
Console.WriteLine(i);
i++; // Add one
Console.WriteLine(i);
i += 2; // Add two
Console.WriteLine(i);
++i; // Add one
Console.WriteLine(i);
i += i; // Multiply by two
Console.WriteLine(i);
//
// Uses increments and assigns.
//
int v = 0;
v = i++; // Increment after value copy
Console.WriteLine(v);
Console.WriteLine(i);
v = ++i; // Increment before value copy
Console.WriteLine(v);
Console.WriteLine(i);
}
}
Output
0 -
1 Added one
3 Added two
4 Added one
8 Added itself
8 Value copy
9 Increment
10 Value copy
10 IncrementUsing post-increment i++. The program begins by declaring a local variable with the identifier 'i' and then uses several increment expressions on it. The i++ statement is also an expression that is executed by loading the value of the variable onto the evaluation stack, adding one to it, and then storing the value into the appropriate slot. The post-increment here begins with the value 0 and changes it to the value 1.
Using post-increment with any constant. The program also shows how you use any constant or other expression resulting in a numeric value in the increment statement. This operation uses the "+=" syntax and is much more flexible than the "++" operator syntax. However, it is longer and possibly harder for other programmers to read if not expected.

Using pre-increment ++i. The C# language also provides a pre-increment operator that uses the two pluses before the identifier. This has equivalent performance on most statements but the order of evaluation is relevant in some cases. When you use pre-increment, the store opcode is executed before the load operation. This means the variable value is changed and then you read it and evaluate it. Register machines must load and store variables in separate steps.
Assigning to increment expression. The final parts of the program show how pre-increment and post-increment on int types affects the order of evaluation. The "v = i++" assignment stores the value of i before the increment occurs. The "v = ++i" assignment stores the value of i after the increment occurs and often this behavior is desired.

The most common use of incrementing a variable is using a for-loop construct. The third statement in the for-loop's statement list usually has an expression such as "i++" that is an integer increment expression. The value of the integer is modified after each successful iteration through the loop (not before).
For Loops
There are some considerations with pre-increment and post-increment on integers related to performance. When you use static fields, the JIT compiler may optimize the expressions less efficiently. This is because the field is not only accessible in the method body. For this reason, you can improve performance by assigning in the same operation as the increment when using fields.
Preincrement IntegersWe saw how int variables can be incremented using a variety of syntaxes in the C# programming language. We saw how you can use the double-plus operator (++) either before or after the variable identifier. We next saw how the pre-increment and post-increment operations are evaluated in different orders and how this affects the result of assignments using increments.
Number Examples