TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Increment Int

This C# article shows how increment works. It covers performance and syntax.

Increment Int. Int values are incremented in several ways.

Some options include the pre-increment and post-increment operators. Incrementing integers is trivial. But there are subtle issues related to the exact order of evaluation.

Decrement Int

Example. Here we increment int variables in different ways using the C# language. The program text shows both the ++ operator (with two pluses) and the += operator (with a plus and an equals).

Also: It shows how putting the pluses before or after the identifier affects the order of evaluation, sometimes in an important way.

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

We use several increment expressions. The i++ statement is 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.

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

Also, the program 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.

Pre-increment has two pluses before the identifier. It has equivalent performance on most statements but the order of evaluation is sometimes relevant. When you use pre-increment, the store opcode is executed before the load operation.

Note: This means the variable value is changed and then you read it and evaluate it.

Reminder: Register machines (which are similar to C# programs) must load and store variables in separate steps.

The program shows how pre-increment and post-increment affect evaluation. The "v = i++" assignment stores the value of "i" before the increment occurs. And the "v = ++i" assignment stores the value after the increment occurs.

For. 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.

Note: The value of the integer is modified after each successful iteration through the loop (not before).

For

Performance. 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.

And: This is because the field is not only accessible in the method body. It is accessible elsewhere in the program.

Preincrement Integers

Tip: For this reason, you can improve performance by assigning in the same operation as the increment when using fields.

Summary. We saw how int variables (numeric value types) can be incremented using a variety of syntaxes in this language. We used the double-plus operator before and after the variable identifier.

Also: We saw how the pre-increment and post-increment operations are evaluated in different orders.

Further: We discovered how this affects the result of assignments using increments.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf