C# Switch with When Clause Example β Smarter Condition Handling!
Hey There! Ready to Level Up Your Switch Case? π―
So, youβve mastered the basic switch case in C#, right? But hereβs the thingβwhat if you need extra conditions inside a switch case? π€
Thatβs where when
Clause comes in! It adds conditions inside case
statements to make your switch even more powerful! π
Letβs dive into this awesome feature with real-world examples! π
π What You Are Going to Learn in This Lesson
β
What is switch with when Clause C# example?
β
Why should you use when
inside a switch?
β
Real-world example with full code + output!
β
How it makes switch cleaner and smarter!
π― What is Switch with When Clause?
A when
Clause in a switch
statement lets you add conditions to specific case
labels. It helps avoid unnecessary nested if-else statements and makes your code easier to read! π
π Syntax of Switch with When Clause in C#
switch (expression)
{
case pattern1 when (condition1):
// Code block 1
break;
case pattern2 when (condition2):
// Code block 2
break;
default:
// Default case
break;
}
β
when (condition)
β Adds extra logic to a case
.
β
Executes only if both pattern & condition match.
π‘ Real-World Example: Movie Ticket Pricing ποΈ
Imagine youβre building a movie ticket system. The price depends on:
π― Age of the customer (kids & seniors get discounts).
π― Time of the day (morning shows are cheaper).
Hereβs how switch with when Clause makes this super easy! π
using System;
class Program
{
static void Main()
{
int age = 65; // Try changing to 10, 30, or 80
string time = "morning"; // Try changing to "evening"
int ticketPrice = (age, time) switch
{
(<= 12, _) => 5, // Kids get $5 tickets anytime
(>= 60, "morning") => 6, // Seniors get $6 tickets in the morning
(>= 60, "evening") => 8, // Seniors pay $8 in the evening
(_, "morning") => 10, // Morning show for adults is $10
(_, "evening") => 15, // Evening show for adults is $15
_ => 12 // Default price
};
Console.WriteLine($"Ticket Price: ${ticketPrice}");
}
}
π Code Breakdown β Whatβs Happening?
- Checks age & time inside a switch expression.
- Uses
_
wildcard when a condition isnβt needed. - Short and clean! No need for long if-else statements.
π¬ Output Examples
If age = 65
and time = "morning"
Ticket Price: $6
(Senior discount for morning shows! π)
If age = 10
and time = "evening"
Ticket Price: $5
(Kids always get the cheapest tickets! π§ποΈ)
If age = 30
and time = "evening"
Ticket Price: $15
(Full price for an evening movie! ππ₯)
π― Why Use Switch with When Clause?
β
Removes unnecessary if-else chains ποΈ
β
Handles multiple conditions easily π οΈ
β
More readable & cleaner code π§Ό
β
Perfect for complex decision-making π€
π₯ Conclusion
See how switch with when Clause C# example makes your code simpler & smarter? You can add extra conditions without messy if-else statements! π―
So, whatβs next? Try modifying the code! Change the age and time, and see how it reacts! Letβs have some fun with it! π
Next What? π
Great job, buddy! π Now that youβve mastered switch with when Clause C# example, letβs level up even more!
π In the next lesson, you will learn Advanced Conditional Statements! Get ready for some next-level C# magic! π₯
Stay curious & keep coding! See you in the next lesson! ππ‘