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
case
andbreak
. - 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
case
orbreak
needed β 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 (
_
) returns5
days.
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! π