Skip to Content
Course content

9: Operators and Expressions

Click on the "Edit" button in the top corner of the screen to edit your slide content.

What's the actual difference between i++ and ++i?

You've probably noticed that both of these add 1 to a variable. In a standalone line like count++;, they are identical. But when you use them inside another expression—like an assignment or a method call—they behave very differently. I've seen plenty of junior devs introduce subtle bugs because they missed this.

The rule is simple: i++ (post-increment) uses the value first and then increments it. ++i (pre-increment) increments the value first and then uses it. Look at this example where we're simulating a simple queue for a game lobby:

int currentGuest = 0;
int guestA = currentGuest++; // guestA is 0, then currentGuest becomes 1
int guestB = ++currentGuest; // currentGuest becomes 2, then guestB is 2

Console.WriteLine($"Guest A: {guestA}, Guest B: {guestB}"); 
// Output: Guest A: 0, Guest B: 2

If you're just looping through an array, you usually won't notice. But if you're using the increment to track an index while simultaneously assigning a value, pick your operator carefully.

I keep seeing ?? and ?. in C# code. What are those doing?

These are some of my favorite tools in C# because they clean up "null-check soup." Instead of writing three nested if statements to make sure an object isn't null before accessing its properties, we use the Null-Conditional operator (?.) and the Null-Coalescing operator (??).

The ?. operator tells C#: "If the thing on the left is null, stop right here and just return null instead of crashing with a NullReferenceException." The ?? operator then lets you provide a fallback value if the result was null.

string? inputUsername = GetUsernameFromDatabase();

// Instead of: if (inputUsername == null) { name = "Guest"; }
// We do this in one line:
string displayName = inputUsername ?? "Guest";

// Or combining them to get the length of a string that might be null:
int? length = inputUsername?.Length; 
int finalLength = length ?? 0;

It makes your code read more like a sentence and less like a defensive checklist.

How does "short-circuiting" actually work with && and ||?

You know that && means "AND" and || means "OR." But the real power is that C# is lazy—in a good way. This is called short-circuiting. If the first part of an && expression is false, C# doesn't even bother looking at the second part, because the whole thing is guaranteed to be false anyway.

I use this constantly to guard against crashes. Look at this line:

if (user != null && user.IsAdmin) 
{ 
    // Grant access 
}

If user is null, the first condition is false. Because of short-circuiting, C# never evaluates user.IsAdmin. If it did, the program would crash instantly. This pattern is a staple in professional C# development; you check for the "safe" condition first, then the "specific" condition second.




📋 Practical Task

Build a Luxury Hotel Room Validator

You are writing the logic for a hotel booking system. You need to create a small program that determines if a guest is eligible for a "VIP Suite" based on three variables: bool isMember, int loyaltyPoints, and string? specialRequest.

Your task is to write a single boolean expression (assigned to a variable called canBookVip) that returns true if:

  • The guest is a member AND has more than 1000 loyalty points.
  • OR, the guest has a special request that is exactly "CEO".
  • OR, the guest is a member and their special request is not null (using the null-coalescing operator to handle the null case).

Ensure you use the short-circuiting behavior to prevent any potential null reference exceptions when checking the specialRequest string.

Rating
0 0

There are no comments for now.

to be the first to leave a comment.