Advertisement
Beginner C# Lesson 4 of 5

Lesson 4: Arithmetic & Assignment Operators in C#

Operators are symbols that tell the compiler to perform specific operations on values. In this lesson, we'll cover the math operators you'll use every day โ€” and the assignment shortcuts that make your code cleaner and faster to write.

Advertisement

Arithmetic Operators

C# supports all standard math operations. Here's a full reference:

OperatorNameExampleResult
+Addition10 + 313
-Subtraction10 - 37
*Multiplication10 * 330
/Division10 / 33 (integer!)
%Modulus (remainder)10 % 31
int a = 10, b = 3;

Console.WriteLine(a + b);  // 13
Console.WriteLine(a - b);  // 7
Console.WriteLine(a * b);  // 30
Console.WriteLine(a / b);  // 3  โ† integer division drops remainder
Console.WriteLine(a % b);  // 1  โ† remainder of 10 รท 3

// For true decimal division, use double or cast first:
double result = (double)a / b;
Console.WriteLine(result);  // 3.3333...

๐Ÿ’ก Modulus trick: n % 2 == 0 checks if n is even. n % 2 == 1 checks if it's odd. Very common in loops and algorithms!

String Concatenation with +

The + operator also joins strings. But mixing types requires care:

string first = "Hello";
string last  = "World";
Console.WriteLine(first + " " + last); // Hello World

// Mixing string and int with +
int score = 95;
Console.WriteLine("Score: " + score); // Score: 95

// Preferred: string interpolation is cleaner
Console.WriteLine($"Score: {score}"); // Score: 95

Increment & Decrement Operators

These operators add or subtract 1. They can be placed before (prefix) or after (postfix) the variable โ€” the difference matters:

int x = 5;

// Postfix: returns current value THEN increments
Console.WriteLine(x++); // Output: 5  (x becomes 6 after)
Console.WriteLine(x);   // Output: 6

// Prefix: increments THEN returns new value
Console.WriteLine(++x); // Output: 7  (x is 7 before printing)

// Decrement works the same way
int y = 10;
y--;  // y is now 9
--y;  // y is now 8

Assignment Operators

Assignment operators combine an arithmetic operation with assignment โ€” shorthand that reduces repetition:

OperatorEquivalent toExample (x = 10)Result
=assignx = 5x = 5
+=x = x + nx += 3x = 13
-=x = x - nx -= 3x = 7
*=x = x * nx *= 3x = 30
/=x = x / nx /= 3x = 3
%=x = x % nx %= 3x = 1
int total = 100;

total += 50;   // total = 150
total -= 25;   // total = 125
total *= 2;    // total = 250
total /= 5;    // total = 50
total %= 7;    // total = 1  (50 mod 7 = 1)

Console.WriteLine(total); // 1

Operator Precedence

When multiple operators appear in one expression, C# evaluates them in a specific order โ€” just like school math (BODMAS/PEMDAS):

int result = 2 + 3 * 4;      // = 2 + 12 = 14  (* before +)
int forced = (2 + 3) * 4;    // = 5 * 4 = 20   (parentheses first)
int mixed  = 10 - 4 / 2 + 1; // = 10 - 2 + 1 = 9

Console.WriteLine(result); // 14
Console.WriteLine(forced); // 20
Console.WriteLine(mixed);  // 9

๐Ÿ’ก Tip: When in doubt, use parentheses to make your intent explicit. It makes code more readable and avoids precedence bugs.

Advertisement

๐Ÿง  Quick Check โ€” Lesson 4

What is the output of: int x = 5; Console.WriteLine(x++);

Lesson Summary

โœ…

C# has 5 arithmetic operators: +, -, *, /, % (modulus/remainder).

โœ…

Integer division drops the decimal โ€” cast to double if you need the fractional result.

โœ…

x++ returns current value then increments; ++x increments first then returns new value.

โœ…

Assignment operators like +=, -=, *= are shorthand for common patterns.

โœ…

* and / have higher precedence than + and -. Use parentheses to override.

Final Lesson

Lesson 5: Comparison & Logical Operators โ€” the foundation of conditions

Next Lesson โ†’