C# Switch Case Example: Simple Guide with Real-World Code
Hey there, future C# ninja! 👋 Ever felt like writing a ton of if-else statements just to check multiple conditions? Well, there’s a better way! Say hello to switch case in C#—a cleaner, faster, and more efficient way to handle multiple choices. 🚀
Before we dive into a real-world example, let’s start with the basics.
What is Switch Case in C#? 🤔
The switch
statement is like a smart decision-maker. It checks a value and matches it with different cases. When it finds a match, it runs that block of code.
Basic Example: Day of the Week 📅
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
Output:
Wednesday
How It Works:
- The program checks the
day
variable. - Since
day = 3
, it matches case 3 and prints “Wednesday”. - If no cases match, the
default
case runs.
Easy, right? Now, let’s use switch case C# in a fun, real-world scenario.
Real-World Example: Coffee Machine ☕
Imagine you own a coffee vending machine. Customers press a number to select their drink. Let’s write a C# switch case example for that!
C# Switch Case Example: Coffee Machine
using System;
class CoffeeMachine
{
static void Main()
{
Console.WriteLine("Select a drink:");
Console.WriteLine("1. Espresso");
Console.WriteLine("2. Cappuccino");
Console.WriteLine("3. Latte");
Console.WriteLine("4. Black Coffee");
Console.Write("Enter your choice (1-4): ");
int choice = Convert.ToInt32(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("You selected Espresso. Enjoy your coffee! ☕");
break;
case 2:
Console.WriteLine("You selected Cappuccino. Enjoy your coffee! ☕");
break;
case 3:
Console.WriteLine("You selected Latte. Enjoy your coffee! ☕");
break;
case 4:
Console.WriteLine("You selected Black Coffee. Enjoy your coffee! ☕");
break;
default:
Console.WriteLine("Invalid choice! Please select a number between 1 and 4.");
break;
}
}
}
Sample Output 1 (User selects 2)
Select a drink:
1. Espresso
2. Cappuccino
3. Latte
4. Black Coffee
Enter your choice (1-4): 2
You selected Cappuccino. Enjoy your coffee! ☕
Sample Output 2 (User enters 5 – an invalid option)
Select a drink:
1. Espresso
2. Cappuccino
3. Latte
4. Black Coffee
Enter your choice (1-4): 5
Invalid choice! Please select a number between 1 and 4.
How Does It Work?
- The program displays coffee options to the user.
- It asks for a number input (1-4).
- Based on the choice, the program prints the selected drink.
- If the user enters something invalid, it shows an error message.
Pretty cool, right? This is why switch case C# is way cleaner than a long if-else ladder! 🎉
Why Use Switch Case Instead of If-Else? 🤷♂️
- Cleaner Code – No messy
if-else if-else
statements. - Faster Execution – Switch is optimized for multiple conditions.
- Easier to Read – Looks structured and neat.
If you’re dealing with multiple fixed values (like menu items, weekdays, or user options), switch case is your best friend!
Conclusion 🎯
Awesome! 🎉 You just learned how to use switch case in C# with a real-world coffee machine example. Now, you can build programs that handle multiple choices in a clean and efficient way!
Next What? 🚀
Hey, you’re doing awesome! 🔥 But guess what? We’re just getting started!
So far, you’ve got the basics of switch case
in C#. But there’s more cool stuff ahead! In the next lesson, we’ll level up and dive into Advanced Switch Case Statements. 🎯
Here’s what you’ll master next:
✅ Switch Expression (C# 8.0+) – A shorter, cleaner approach ✂️
✅ Pattern Matching in switch (C# 9.0+) – Smarter decision-making 🎯
✅ Switch with when Clause – Adding conditions for more control 🔍
You’ll learn how to write switch cases like a pro, making your code cleaner, faster, and smarter! 😎
👉 Excited? Ready to level up? Let’s crush it in the next lesson! 🚀