C# Switch Expression – A Shorter, Cleaner Way!
Hey there, C# learner! 👋 Ever felt frustrated writing long switch statements with all those case and break keywords? 😵 Traditional switch cases work fine, but they take up too much space and look repetitive.
Good news! C# 8.0 introduced the switch Expression – a shorter and cleaner way to write switch cases. 🎉 No more endless break statements!
Why Use Switch Expression? 🤔
- Less code, more readability – No need for
caseandbreak. - Faster execution – It’s optimized for better performance.
- More fun to write! – Who doesn’t love cleaner code? 😎
Syntax of Switch Expression in C#
Alright, buddy! Before we jump into more examples, let’s crack the syntax of the switch Expression in C#. Don’t worry—it’s super simple! 😎
Here’s what it looks like:
variable = input switch
{
case1 => result1,
case2 => result2,
case3 => result3,
_ => defaultResult // Default case (optional)
};
Breaking it Down Like a Pro 🔍
- No
caseorbreakneeded – Just writecaseValue => result. - Each case is separated by a comma (
,) – No{}or:needed! - The
_(underscore) means “default case” – It handles all unmatched cases.
Now, let’s turn this into a real-life example!
Example: Convert Numbers to Words
int number = 3;
string word = number switch
{
1 => "One",
2 => "Two",
3 => "Three",
_ => "Unknown"
};
Console.WriteLine(word);
Output (if number = 3):
Three
Pretty cool, right? 😃 No case, no break, just clean code!
Why is Switch Expression Better?
✅ Less typing – Saves time and effort!
✅ Easier to read – No case blocks cluttering your code.
✅ No missing break errors – One less thing to worry about!
Now that you know the syntax, go ahead and try it in your own programs! 🚀
Real-World Example: Order Processing System
Imagine you are building an e-commerce website. Users order different items, and each item has a delivery time:
- Electronics take 7 days 📱
- Clothing takes 3 days 👕
- Books take 2 days 📚
- Other items take 5 days 📦
Instead of writing a long switch statement, let’s simplify it with a switch Expression in C#!
C# Switch Expression Example 📌
using System;
class Program
{
static void Main()
{
string itemCategory = "Electronics"; // Try changing this to "Books" or "Clothing"
int deliveryDays = itemCategory switch
{
"Electronics" => 7,
"Clothing" => 3,
"Books" => 2,
_ => 5 // Default case for other items
};
Console.WriteLine($"Your {itemCategory} will be delivered in {deliveryDays} days.");
}
}
Output (if itemCategory = "Electronics"):
Your Electronics will be delivered in 7 days.
How Does This Code Work? 🧐
- The user selects an item category (e.g.,
"Electronics"). - The switch Expression checks the category and returns the delivery time.
- No need for
case,break, orreturn. Just clean, simple code! - If the category doesn’t match, the default case (
_) returns5days.
See how much cleaner this is compared to a traditional switch? 🚀
Conclusion 🎯
The switch Expression in C# is a game-changer for writing short, clean, and readable code. Instead of using traditional switch statements, you can save time and avoid unnecessary lines of code.
Next time you write a switch case, ask yourself: Can I use a switch Expression instead? If yes, go for it! 😎
Next What? 🚀
You’re doing great! 🔥 But there’s more awesomeness ahead.
In the next lesson, we’ll explore Pattern Matching – a powerful way to make your switch statements even smarter! 🧠
👉 Excited? Let’s dive into Pattern Matching next! 🚀
