Assignment operators put values into variables. You've already seen the basic = operator, but C# has shortcut versions that combine assignment with arithmetic. They save typing and make your intentions clearer.
The Simple Assignment Operator (=)
The single equals sign stores a value in a variable. The right side is evaluated first, then the result is stored in the left-side variable:
int x = 10; // x gets 10
int y = x + 5; // y gets 15 (x+5 is evaluated first)
Compound Assignment Operators
When you want to modify a variable and store the result back in the same variable, compound operators are your friend:
int score = 100;
score += 10; // score = score + 10 → 110
score -= 20; // score = score - 20 → 90
score *= 2; // score = score * 2 → 180
score /= 3; // score = score / 3 → 60
score %= 7; // score = score % 7 → 4
Console.WriteLine(score); // 4
Each compound operator does the operation using the current value, then stores the result back. score += 10 is exactly the same as score = score + 10, just shorter.
Why Use Compound Operators?
They're not just shorter — they also make it clear that you're modifying an existing value rather than creating something new. Plus, they reduce the chance of typos since you only write the variable name once:
// Without compound operator
totalPrice = totalPrice + tax;
totalPrice = totalPrice - discount;
// With compound operator — cleaner and less error-prone
totalPrice += tax;
totalPrice -= discount;
Compound operators work with all numeric types — int, double, decimal, float, and even long.
Putting It All Together
Here's a real-life scenario: tracking a player's score in a game:
int score = 0;
score += 100; // Killed an enemy
score += 50; // Collected a bonus
score *= 2; // Double points power-up!
score -= 25; // Bought an item
Console.WriteLine($"Final score: {score}"); // 275
See how natural that reads? The compound operators make the logic of the game clear just from how the numbers change.