Lesson 27 min read

Operators & Expressions

The verbs of your code — making things happen

What Are Operators?

If variables are the nouns in your code, operators are the verbs. They do stuff — add numbers, compare values, combine conditions. Mastering them is your first step toward understanding algorithm complexity. Think of them as the action words that make your program actually do something instead of just sitting there holding data.

C# has several families of operators, and you'll use most of them every day.

Arithmetic Operators — Math Class

int a = 17, b = 5;
Console.WriteLine($"Add: {a + b}"); // 22
Console.WriteLine($"Subtract: {a - b}"); // 12
Console.WriteLine($"Multiply: {a * b}"); // 85
Console.WriteLine($"Divide: {a / b}"); // 3 (integer division!)
Console.WriteLine($"Remainder: {a % b}"); // 2 (modulus)
// Watch out: int / int = int (no decimals!)
double exact = (double)a / b;
Console.WriteLine($"Exact div: {exact}"); // 3.4
// Increment & decrement
int score = 0;
score++; // score is now 1
score += 10; // score is now 11
score--; // score is now 10
Console.WriteLine($"Score: {score}");
Output
Add:       22
Subtract:  12
Multiply:  85
Divide:    3
Remainder: 2
Exact div: 3.4
Score: 10

Comparison & Logical Operators

These operators ask yes-or-no questions and give back true or false.

  • == equal to (double equals — single = is assignment!)
  • != not equal to
  • <, >, <=, >= — comparisons
  • && — AND (both must be true)
  • || — OR (at least one must be true)
  • ! — NOT (flips true ↔ false)

Think of && as a strict parent ("You need BOTH clean room AND finished homework") and || as a chill parent ("Either one is fine").

Comparisons & Logic

int age = 15;
bool hasTicket = true;
// Comparison operators
Console.WriteLine($"Is teenager: {age >= 13 && age <= 19}");
Console.WriteLine($"Can vote: {age >= 18}");
Console.WriteLine($"Not a baby: {age != 1}");
// Logical operators
bool canRide = age >= 12 && hasTicket;
Console.WriteLine($"Can ride: {canRide}");
bool freeEntry = age < 5 || age > 65;
Console.WriteLine($"Free entry: {freeEntry}");
bool notAllowed = !hasTicket;
Console.WriteLine($"Blocked: {notAllowed}");
Output
Is teenager: True
Can vote:    False
Not a baby:  True
Can ride:    True
Free entry:  False
Blocked:     False

The Ternary Operator & Null Tricks

The ternary operator ? : is a tiny if/else crammed into one line. It's perfect for simple choices:

condition ? valueIfTrue : valueIfFalse

C# also has two amazing null-safety operators:

  • ??null-coalescing: "Use this value, OR if it's null, use that backup value instead."
  • ?.null-conditional: "Only access this property if the object isn't null. Otherwise, just give me null."
  • ispattern matching: checks the type AND can pull out the value in one step.

Ternary, Null Operators & Pattern Matching

// Ternary operator
int temp = 35;
string weather = temp > 30 ? "Hot! Stay inside" : "Nice day!";
Console.WriteLine(weather);
// Null-coalescing ??
string? username = null;
string displayName = username ?? "Anonymous";
Console.WriteLine($"Welcome, {displayName}");
// Null-conditional ?.
string? message = null;
int? length = message?.Length; // null, not a crash
Console.WriteLine($"Length: {length ?? 0}");
// Pattern matching with 'is'
object mystery = 42;
if (mystery is int number)
{
Console.WriteLine($"It's a number: {number * 2}");
}
object greeting = "Hello";
if (greeting is string text && text.Length > 3)
{
Console.WriteLine($"Long greeting: {text}");
}
Output
Hot! Stay inside
Welcome, Anonymous
Length: 0
It's a number: 84
Long greeting: Hello
Note: 💡 The ?? operator is your bodyguard against null. Instead of writing a whole if/else to check for null, just write: var name = input ?? "default"; — one line, zero crashes.

Quick check

What is the result of: 17 / 5 in C# (both are int)?
Variables & Data TypesStrings & Interpolation