Increment. In C# programs int values are incremented in several ways. Some options include the pre-increment and post-increment operators. We can also decrement.
There are subtle issues related to the order of evaluation. And performance can be affected by how we specify these operations.
Example. Here we increment int variables in different ways. The program shows both the ++ operator (with 2 pluses) and the += operator (with a plus and an equals).
Part 1 The post-increment here begins with the value 0 and changes it to the value 1.
Part 2 You use any constant or other expression resulting in a numeric value in the increment statement.
Part 3 Pre-increment has 2 pluses before the identifier. The store opcode is executed before the load operation.
Part 4 We can increment by a variable instead of a constant. Here we increment a number by itself.
Part 5 We can also assign to the result of a post-increment or pre-increment expression.
using System;
int i = 0;
Console.WriteLine(i);
// Part 1: post-increment.
i++;
Console.WriteLine(i);
// Part 2: use constant in increment.
i += 2;
Console.WriteLine(i);
// Part 3: use pre-increment.
++i;
Console.WriteLine(i);
// Part 4: increment with a variable.
i += i;
Console.WriteLine(i);
// Part 5: use increment and assignment.
int v = 0;
v = i++;
Console.WriteLine(v);
Console.WriteLine(i);
v = ++i;
Console.WriteLine(v);
Console.WriteLine(i);0
1
3
4
8
8
9
10
10
Preincrement, part 1. You should be familiar with the "++" and "minus minus" operators. This example uses the 2 pluses at the end of the variable identifier to increment the integer.
Note You can sometimes combine assignments with pre-increments. This can improve performance.
class Program
{
static int _x;
static void Main()
{
// Add one.
_x++;
// Read in the value again.
int count = _x;
}
}
Preincrement, part 2. We can pre-increment an integer. There are cases when accessing an integer is relatively slow. This can be the case with static variables and volatile member ints.
Here We use pre-increment to combine 2 statements into 1 statement. This is shorter code.
class Program
{
static int _x;
static void Main()
{
// Increment then assign.
int count = ++_x;
}
}
Preincrement, benchmark. We can optimize increments by loading the value in the same expression. If we use 2 statements, the static variable must be accessed twice.
Version 1 We increment, and then load the value of the static int. Memory is accessed twice.
Version 2 We increment and load the field in the same expression. Memory is only accessed once.
Result It is faster to combine the increment and read instructions. This is true with .NET 5 in 2021 (on Linux).
Important For local variables, which are faster than fields, this may not have the same performance benefit.
using System;
using System.Diagnostics;
class Program
{
static int _test;
const int _max = 100000000;
static void Main()
{
_test = 0;
var s1 = Stopwatch.StartNew();
// Version 1: increment then load.
for (int i = 0; i < _max; i++)
{
_test++;
int z = _test;
if (z == 0)
{
return;
}
}
s1.Stop();
_test = 0;
// Version 2: increment and load in same expression.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
int z = ++_test;
if (z == 0)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
}
}1.49 ns increment, then load
1.31 ns preincrement and load in single statement
Decrement. This reduces the value of a number. The decrement operator receives one or two operands. It is written with the characters "minus minus."
Here We start the value stored in the variable location with the integer 100. Then, the single decrement operator is applied.
And This decreases the value by one. Finally we use the "-=" operator and use several operands with it.
using System;
int i = 100;
// Decrement by one.
i--;
Console.WriteLine(i);
// Decrement by two.
i -= 2;
Console.WriteLine(i);
// Decrement by negative one (add one).
i -= -1;
Console.WriteLine(i);
// Decrement by zero (do nothing).
i -= 0;
Console.WriteLine(i);
// Decrement by itself (results in zero).
i -= i;
Console.WriteLine(i);99
97
98
98
0
Pre, post decrement. There are 2 forms of the decrement by one operator: post-decrement and pre-decrement. These forms are unary operators—they can only receive one operand.
Detail When you use the post-decrement operator in an expression, the expression is evaluated before the decrement occurs.
And When you use the pre-decrement operator, it is evaluated after. This difference is important.
using System;
int i = 5;
// This evaluates to true.// ... The decrement occurs after the comparison.
if (i-- == 5)
{
Console.WriteLine(true);
}
// This evaluates to true.// ... The decrement occurs before the comparison.
if (--i == 3)
{
Console.WriteLine(true);
}True
True
For. For-loops often have an increment statement. The value of the integer is modified after each successful iteration through the loop (not before).
Summary. Int variables (numeric value types) can be incremented using a variety of syntaxes in C#. Pre-increment and post-increment operations are evaluated in different orders.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 26, 2023 (edit).