04. Operators
Operators are the symbols that combine values into new values: +, ==, &&, and friends. Together with values and variables they form expressions, pieces of code that produce a result.
Arithmetic
The usual math, plus % (modulo) which gives the remainder of a division. Watch out: dividing two integers gives an integer, and the fraction is thrown away. Cast one side to double when you need the decimals.
int a = 10;
int b = 3;
Console.WriteLine($"{a} + {b} = {a + b}");
Console.WriteLine($"{a} - {b} = {a - b}");
Console.WriteLine($"{a} * {b} = {a * b}");
Console.WriteLine($"{a} / {b} = {a / b}");
Console.WriteLine($"{a} % {b} = {a % b}");
Console.WriteLine($"(double){a} / {b} = {(double)a / b}");10 + 3 = 13 10 - 3 = 7 10 * 3 = 30 10 / 3 = 3 10 % 3 = 1 (double)10 / 3 = 3.3333333333333335
Increment and compound assignment
++ adds 1 and -- subtracts 1. Where you put them matters when the result is used: x++ returns the old value, then increments. ++x increments first. Compound operators like += are shorthand for total = total + 20.
int x = 5;
int post = x++;
Console.WriteLine($"post = {post}, x = {x}");
int y = 5;
int pre = ++y;
Console.WriteLine($"pre = {pre}, y = {y}");
int total = 100;
total += 20;
total -= 5;
total *= 2;
total /= 10;
Console.WriteLine($"total = {total}");post = 5, x = 6 pre = 6, y = 6 total = 23
Comparison and logical operators
Comparisons produce a bool. Logical operators combine bools.
| Operator | Meaning |
|---|---|
== != | equal, not equal |
< <= > >= | less / greater (or equal) |
&& | AND: true only if both sides are true |
|| | OR: true if at least one side is true |
! | NOT: flips true and false |
int age = 20;
bool hasTicket = true;
bool isBanned = false;
Console.WriteLine($"age >= 18: {age >= 18}");
Console.WriteLine($"age == 21: {age == 21}");
Console.WriteLine($"age != 21: {age != 21}");
Console.WriteLine($"age >= 18 && hasTicket: {age >= 18 && hasTicket}");
Console.WriteLine($"age < 18 || hasTicket: {age < 18 || hasTicket}");
Console.WriteLine($"!isBanned: {!isBanned}");age >= 18: True age == 21: False age != 21: True age >= 18 && hasTicket: True age < 18 || hasTicket: True !isBanned: True
Short-circuit evaluation
&& stops as soon as the left side is false, and || stops as soon as it is true. The right side is never evaluated. This lets you put a safety check first, so the risky part only runs when it is safe.
int[] items = [];
if (items.Length > 0 && items[0] == 42)
Console.WriteLine("First item is 42");
else
Console.WriteLine("Empty list, items[0] was never read");Empty list, items[0] was never read
Precedence
Like in math, * and / run before + and -, and && runs before ||. Do not memorize the full table: when in doubt, add parentheses. They make intent obvious to readers too.
int withoutParens = 2 + 3 * 4;
int withParens = (2 + 3) * 4;
Console.WriteLine(withoutParens);
Console.WriteLine(withParens);14 20
Null operators (a preview)
null means "no value". Adding ? to a type, as in string?, says the variable may be null. Two operators make null easy to handle:
a ?? bgivesa, orbifais null.a?.LengthreadsLengthonly ifais not null, otherwise the whole expression is null instead of crashing.
string? nickname = null;
string display = nickname ?? "Guest";
int? length = nickname?.Length;
Console.WriteLine($"Hello, {display}");
Console.WriteLine($"Length: {length ?? 0}");Hello, Guest Length: 0
The intermediate course covers null safety in depth.
You will use these constantly in endpoints: ?? for default values of optional query parameters, and && / || in validation checks and database filters.
app.MapGet("/products", (int? page, int? pageSize) =>
{
var currentPage = page ?? 1;
var size = pageSize ?? 20;
return $"Page {currentPage}, {size} per page";
});