C# Increment and Decrement Operators

The increment (++) and decrement (--) operators are used to increase or decrease the value of a variable by 1.

Key Topics

Increment Syntax

The increment operator increases a variable by 1. It can be used in two ways:

++variable;  // Pre-increment
variable++;  // Post-increment

Example: Increment Operator

int x = 5;
x++;
Console.WriteLine(x);  // Outputs 6

Output:

6

Decrement Syntax

The decrement operator decreases a variable by 1. It can also be used in two ways:

--variable;  // Pre-decrement
variable--;  // Post-decrement

Example: Decrement Operator

int y = 5;
y--;
Console.WriteLine(y);  // Outputs 4

Output:

4

Key Takeaways

  • The ++ operator increments a variable by 1.
  • The -- operator decrements a variable by 1.