Skip to main content

How to Use the Increment Operator Before and After a Variable in C#

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

count++ adds 1 to count. Inside a bigger expression, the place of the ++ decides the value you get: a++ gives the value from before the addition, ++a the value after it.

Microsoft's reference: The unary increment operator ++ increments its operand by 1. The operand must be a variable, a property access, or an indexer access. For the two forms: The result of x++ is the value of x before the operation, and The result of ++x is the value of x after the operation.

Syntax​

Syntax
<variable>++;
++<variable>;

<type> <result> = <variable>++;
<type> <result> = ++<variable>;
PartWhat it is
<variable>++Postfix: adds 1, and the expression's value is the old one
++<variable>Prefix: adds 1, and the expression's value is the new one
On a line of its ownBoth forms just add 1

The Example​

A console app called Increment uses a++ and ++c in assignments and prints the variables, then counts to 2 with count++.

Prerequisites​

  • The .NET 10 SDK. dotnet --version prints a version that starts with 10.0.

Step 1: Create a Console App​

Terminal
dotnet new console -n Increment
cd Increment
Output
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/you/source/Increment/Increment.csproj:
Determining projects to restore...
Restored /Users/you/source/Increment/Increment.csproj (in 53 ms).
Restore succeeded.

dotnet new console -n Increment creates a folder named Increment with a project file and a Program.cs, and cd moves into it. Everything this example uses is part of .NET, so there's no package to add. Open the folder in VS Code, or Increment.csproj in Visual Studio.

Step 2: Write the Code​

Replace everything in Program.cs. First the postfix form:

Program.cs (postfix)
int a = 5;
int b = a++; // b gets the old value, then a goes up.
Console.WriteLine($"After b = a++: a = {a}, b = {b}");

Then the prefix form:

Program.cs (prefix)
int c = 5;
int d = ++c; // c goes up first, then d gets the new value.
Console.WriteLine($"After d = ++c: c = {c}, d = {d}");

And ++ on a line of its own:

Program.cs (on its own)
int count = 0;
count++;
count++;
Console.WriteLine($"After two count++: count = {count}");

Step 3: Run It​

Run it in VS Code's terminal, or press Ctrl+F5 in Visual Studio:

Terminal
dotnet run
Output
After b = a++: a = 6, b = 5
After d = ++c: c = 6, d = 6
After two count++: count = 2

macOS-style terminal window titled Terminal — Increment showing dotnet run printing After b = a++: a = 6, b = 5, then After d = ++c: c = 6, d = 6, then After two count++: count = 2

a and c both went from 5 to 6. b got 5, the value from before the addition, and d got 6, the value after it.

The Complete File​

macOS-style code window titled Program.cs — Increment showing b = a++ and d = ++c on variables that start at 5, with comments on which value each one gets, and a count increased twice with count++

Program.cs
int a = 5;
int b = a++; // b gets the old value, then a goes up.
Console.WriteLine($"After b = a++: a = {a}, b = {b}");

int c = 5;
int d = ++c; // c goes up first, then d gets the new value.
Console.WriteLine($"After d = ++c: c = {c}, d = {d}");

int count = 0;
count++;
count++;
Console.WriteLine($"After two count++: count = {count}");

Common Mistakes​

MistakeWhat happensDo this instead
++ on a number or other value5++ doesn't build: error CS1059: The operand of an increment or decrement operator must be a variable, property or indexerPut the value in a variable first
Assigning x++ back to xx = x++; left x at 5: x++ adds 1, then the assignment puts back the old value it gaveWrite just x++;

FAQ​

How do I subtract 1 instead?​

Use --, which works the same way before or after a variable: y-- and then --y took y from 5 to 3.

Conclusion​

++ adds 1 to a variable. On a line of its own, x++ and ++x do the same thing; inside an expression, x++ gives the old value and ++x the new one.

Additional Resources​