Complete C# Tutorial

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 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! πŸš€πŸ’‘

Leave a Comment

Share this Doc

Switch with when Clause

Or copy link