C# Pattern Matching – The Smart Way to Make Decisions 🤖
Hey there, C# learner! 👋 Ever struggled with writing if-else chains or nested switch cases just to check types or conditions? 😵
Well, C# 9.0+ has something cool for you—Pattern Matching! 🎉 It makes your code smarter, shorter, and cleaner.
Imagine you have different types of data (like numbers, strings, objects), and you need to handle them differently. Instead of writing messy if-else statements, Pattern Matching lets you match data directly inside a switch expression! 🚀
What You Are Going to Learn in This Lesson 🎯
✅ What is Pattern Matching in C#?
✅ How it makes your code smarter
✅ Pattern Matching Syntax with Explanation
✅ Real-world example: smart response system
✅ Complete code with explanation and output
By the end, you’ll write cleaner and more efficient code like a pro! 😎
Why Use Pattern Matching? 🤔
✅ No need for extra type checks – C# does it for you!
✅ Less code, more readability – No more nested if-else.
✅ Works with numbers, strings, objects, and more!
Syntax of Pattern Matching in C#
Here’s how Pattern Matching works inside a switch statement:
switch (variable)
{
case Type1 varName1:
// Code for Type1
break;
case Type2 varName2 when (condition):
// Code for Type2 with condition
break;
case Type3:
// Code for Type3
break;
default:
// Default case (if no match)
break;
}
Breaking it Down 🧐
variable→ The value being checked.case Type1 varName1→ Matches ifvariableis ofType1.case Type2 varName2 when (condition)→ MatchesType2only if thewhencondition is true.case Type3→ Matches specific values or types.default→ If nothing matches, this block executes.
Example: Pattern Matching in Switch Case (Checking Different Types) 🚀
Let’s say you’re building a smart response system that reacts differently based on the type of input.
using System;
class Program
{
static void Main()
{
object input = 42; // Try changing to "Hello" or 3.14
switch (input)
{
case int number when number > 0:
Console.WriteLine("Positive Number ✅");
break;
case int number when number < 0:
Console.WriteLine("Negative Number ❌");
break;
case string text:
Console.WriteLine($"It's a string: {text} ✍️");
break;
case double decimalNumber:
Console.WriteLine($"It's a decimal number: {decimalNumber} 🔢");
break;
default:
Console.WriteLine("Unknown Type 🤷♂️");
break;
}
}
}
Output Examples (Based on Input)
If input = 42
Positive Number ✅
If input = "Hello"
It's a string: Hello ✍️
If input = 3.14
It's a decimal number: 3.14 🔢
Key Features of Pattern Matching in Switch Case
- Checks types dynamically (e.g.,
int,string,double). - Uses
whento add conditions inside cases. - No need for type casting! 🚀
- Handles multiple cases smoothly without long
if-elsechains.
Final Thoughts 💡
Now you know how Pattern Matching in Switch Case makes your code smarter, cleaner, and more powerful! 🎯
👉 Try changing the input value and see how it reacts! Which type will you test next? Let’s have fun coding! 😃🚀
Conclusion 🎯
Pattern Matching in C# saves you time and effort by letting you match types directly inside a switch expression. Instead of writing long if-else statements, you can write cleaner and smarter code! 🚀
Next time you need to check an object’s type, ask yourself: Can I use Pattern Matching instead? If yes, go for it! 😎
Next What? 🚀
Great job! 🎉 You’re getting better and better at writing smart C# code!
In the next lesson, you’ll learn about Switch with When Clause – a powerful way to add extra conditions inside a switch case! 🧐
👉 Excited? Let’s jump to the next lesson! 🚀
