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! π
