Advanced Conditional Features in C# – A Complete Guide!
By now, you’ve got a solid grip on if-else and switch-case, right? But guess what? There’s more! 🎉
C# gives you powerful conditional features that make your code shorter, cleaner, and smarter! 🤓
So, let’s explore these awesome features one by one! 🚀
📚 What You Are Going to Learn in This Lesson
✅ &&
(Logical AND) and ||
(Logical OR) – Combine multiple conditions!
✅ ??
(Null-Coalescing) and ?.
(Null-Conditional) – Avoid NullReferenceException
like a pro!
✅ goto case
and goto default
– Jump between cases in a switch!
✅ Short-Circuit Evaluation – Make your code more efficient!
🟢 &&
(Logical AND) and ||
(Logical OR) in C#
These operators help us combine multiple conditions inside an if
statement.
&&
(AND) → Both conditions must be true.||
(OR) → At least one condition should be true.
🚗 Real-World Example: Checking Car Rental Eligibility
Let’s say a person needs to rent a car. The conditions:
✔ Age should be at least 21.
✔ Must have a valid driving license.
using System;
class Program
{
static void Main()
{
int age = 22;
bool hasLicense = true;
if (age >= 21 && hasLicense)
{
Console.WriteLine("✅ You can rent a car!");
}
else
{
Console.WriteLine("❌ Sorry, you cannot rent a car.");
}
}
}
🖥 Output
✅ You can rent a car!
🎯 Modify age
and hasLicense
to test different cases!
🟢 ??
(Null-Coalescing) and ?.
(Null-Conditional) in C#
One of the most annoying errors in C#? NullReferenceException! 😡
To avoid this, C# gives us two lifesavers:
?.
(Null-Conditional Operator) → Prevents accessing properties/methods onnull
.??
(Null-Coalescing Operator) → Provides a default value if something isnull
.
📱 Real-World Example: Handling Missing Usernames
using System;
class Program
{
static void Main()
{
string? userName = null; // Change to "Steven" and test!
// ?. prevents calling .ToUpper() on null
Console.WriteLine($"Hello, {userName?.ToUpper() ?? "Guest"}!");
}
}
🖥 Output
Hello, Guest!
Try setting userName = "Steven"
and see what happens!
👉 Want to master ??
and ?.
like a pro? Head over to our in-depth guide and level up your C# skills!
🟢 goto case
and goto default
in C#
Sometimes, in a switch-case, we need to jump to another case without breaking. That’s where goto case
and goto default
come in!
🎮 Real-World Example: Gaming Levels System
using System;
class Program
{
static void Main()
{
int level = 2; // Try changing to 1, 3, or any other value!
switch (level)
{
case 1:
Console.WriteLine("Beginner Level 🎮");
break;
case 2:
Console.WriteLine("Intermediate Level 🔥");
goto case 1; // Jump to case 1
case 3:
Console.WriteLine("Advanced Level 🚀");
goto default; // Jump to default case
default:
Console.WriteLine("Unknown Level ❓");
break;
}
}
}
🖥 Output (if level = 2
)
Intermediate Level 🔥
Beginner Level 🎮
Notice how goto case 1;
jumped to the Beginner Level!
🟢 Short-Circuit Evaluation in C#
Ever heard of lazy evaluation? That’s exactly what short-circuiting does! 🚀
- In
&&
(AND), if the first condition isfalse
, it won’t check the second! - In
||
(OR), if the first condition istrue
, it won’t check the second!
🏃♂️ Real-World Example: Speed Test
using System;
class Program
{
static bool IsFast()
{
Console.WriteLine("Checking speed...");
return false;
}
static void Main()
{
bool isRunning = false;
if (isRunning && IsFast()) // Short-circuits! Doesn't call IsFast()
{
Console.WriteLine("🏃♂️ Fast mode enabled!");
}
else
{
Console.WriteLine("🚶♂️ Normal mode.");
}
}
}
🖥 Output
🚶♂️ Normal mode.
Notice “Checking speed…” never printed? That’s because isRunning
is false
, so &&
didn’t even bother checking IsFast()
! 🔥
🔥 Conclusion
You just unlocked some of the coolest advanced conditional features in C#! 🚀
✅ Logical AND &&
and OR ||
– Combine multiple conditions easily.
✅ ??
and ?.
– Save yourself from annoying NullReferenceException
.
✅ goto case
and goto default
– Jump between switch cases.
✅ Short-Circuit Evaluation – Speed up your conditions!
Now it’s time for you to try these out! Modify the examples, break things, and learn by doing! 🎯
Next What? 🚀
You’re on fire! 🔥 Next up, we’ll dive even deeper into Advanced Conditional Statements & Expressions in C#! Stay tuned! 🚀
💬 Got stuck? Have questions? Let me know in the comments! Let’s keep learning together! 🎉