C#
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented C#
-
Section 4: Working with Data
-
Section 5: Error Handling
-
Section 6: Delegates and Events
-
Section 7: Async Programming
-
Section 8: More Language Features
-
Section 9: File I/O and Serialization
-
Section 10: Networking in .NET
-
Section 11: The .NET Ecosystem
-
Section 12: Memory and Performance
-
Section 13: Concurrency Beyond Async
-
Section 14: Reflection and Attributes
-
Section 15: Testing and Best Practices
-
Section 16: Design Patterns in C#
-
Section 17: Standard Library Deep Dive
-
Section 18: Data Structures and Algorithms in C#
-
Section 19: GUI and Desktop Development Overview
-
Section 20: Practical Projects
-
Section 21: More Practice Exercises
-
Section 22: More Standard Library and Text Processing
-
Section 23: More Design Patterns
-
Section 24: More Projects
-
Section 25: Interview Practice
-
Section 26: C# Keywords Reference (Modifiers)
-
Section 27: C# Keywords Reference (Statements)
-
Section 28: C# Keywords Reference (Operators)
-
Section 29: BCL: System.Collections.Generic
-
Section 30: BCL: System.Linq
-
Section 31: BCL: System.Threading
-
Section 32: BCL: System.IO
-
Section 33: BCL: System.Text and System.Text.Json
-
Section 34: BCL: System.Net.Http
-
Section 35: C# Language Specification Topics
-
Section 36: More Practice Exercises
-
Section 37: More Async Patterns
-
Section 38: More BCL: System.Reflection and System.Diagnostics
-
Section 39: Nullable Reference Types In Depth
-
Section 40: C# Records and Pattern Matching In Depth
-
Section 41: Dependency Injection Deep Dive
-
Section 42: More Interview and Whiteboard Practice
9: Operators and Expressions
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.
There are no comments for now.