HomeSearch

C# Increment, Preincrement and Decrement Ints

Use increment, preincrement and decrement on ints. See benchmarks for these operations.
Increment. 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 exact 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).

Note: The post-increment here begins with the value 0 and changes it to the value 1.

Also: You use any constant or other expression resulting in a numeric value in the increment statement.

Pre-increment: Pre-increment has 2 pluses before the identifier. The store opcode is executed before the load operation.

Info: With pre-increment, the variable value is changed and then you read it and evaluate it.

C# program that increments integers 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 Increment
For. For-loops often have an increment statement. The value of the integer is modified after each successful iteration through the loop (not before).For
Notes, performance. When you use static fields, the JIT compiler may optimize the expressions less efficiently. With statics, combining an increment into an assignment may help.
Preincrement. A number is incremented with an operator. Two increment operators in the C# language are postincrement and preincrement. They have different meanings.
Preincrement: ++i; Postincrement: i++;
Preincrement, part 1. You should be familiar with the "++" and "—" operators. This example uses the two pluses at the end of the variable identifier to increment the integer.Int, uint

Note: You can sometimes combine assignments with preincrements. This can improve performance.

C# program that postincrements class Program { static int _x; static void Main() { // Add one. _x++; // Read in the value again. int count = _x; } }
Preincrement, part 2. We can preincrement 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 preincrement to combine 2 statements into 1 statement. This is shorter code.

Program 2: C# class Program { static int _x; static void Main() { // Increment then assign. int count = ++_x; } }
Preincrement, benchmark. When all variables are easy to optimize, there is no difference between postincrement and preincrement. If your int is another class, it's slower to use.
Lines of code in benchmark // // 1. // TestStatic._test++; int z = TestStatic._test; // // 2. // int z = ++TestStatic._test; Benchmark results Method 1 postincrement: 3026 ms Method 2 preincrement: 2574 ms
Decrement. This reduces the value of a number. The decrement operator receives one or two operands. It is written with the characters "minus minus" or "-=".

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.

C# program that uses decrement operators using System; class Program { static void Main() { 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); } } Output 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.

Order: 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.

C# program that uses two forms of decrement using System; class Program { static void Main() { 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); } } } Output True True
A 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.
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls