What Is Pattern Matching?
Cássio Huggentobler de Costa
Software Engineer | Tech Lead | .NET & C# | SQL & NoSQL | Google Cloud | ETL & Data visualization (Looker Studio, PowerBI)
Imagine you’re organizing a Halloween party. You put pumpkins by the door, costumes in one room, and candy on the snack table. Suddenly, someone brings in something unusual—a baseball cap! You think, “Hmm, that’s not Halloween-y,” and toss it into the “random stuff” bin. In C#, Pattern Matching is like having a helper who does this kind of sorting for your code! It helps your program understand what each item is and what to do with it, making everything simpler and easier to manage.
How Does It Work?
In C#, Pattern Matching acts like a Halloween item sorter:
Let’s see how this works in a fun example.
Simplifying Decisions with switch and Pattern Matching
Imagine you have a bunch of Halloween items to sort. Depending on what each item is, you have different plans:
With Pattern Matching, switch can handle all this easily:
public void HandleHalloweenItem(object item)
{
switch (item)
{
case Pumpkin pumpkin:
Console.WriteLine("Placing pumpkin by the entrance.");
break;
case Candy candy when candy.IsSpecial:
Console.WriteLine($"Putting special candy {candy.Name} on the fancy table!");
break;
case Candy candy:
Console.WriteLine($"Putting regular candy {candy.Name} on the snack table.");
break;
case Decoration decor:
Console.WriteLine("Hanging decoration on the wall.");
break;
case null:
Console.WriteLine("No item to handle.");
break;
default:
Console.WriteLine("Unknown item. Ignoring it.");
break;
}
}
Here’s what this switch does at the Halloween party:
领英推荐
Using Pattern Matching with if
Pattern Matching also works with if statements, which can make things even simpler:
if (item is Pumpkin)
{
Console.WriteLine("Placing pumpkin by the entrance.");
}
else if (item is Candy candy && candy.IsSpecial)
{
Console.WriteLine($"Putting special candy {candy.Name} on the fancy table.");
}
else if (item is Candy candy)
{
Console.WriteLine($"Putting regular candy {candy.Name} on the snack table.");
}
else if (item is Decoration)
{
Console.WriteLine("Hanging decoration on the wall.");
}
else
{
Console.WriteLine("Unknown item. Ignoring it.");
}
With if, Pattern Matching still identifies each item type and takes the right action—a real time-saver!
Why Use Pattern Matching?
Wrapping Up
Pattern Matching in C# is like having a Halloween party assistant who knows exactly what to do with pumpkins, candy, and decorations without you having to keep explaining. It keeps your code organized and is an awesome way to simplify complex conditions.
How about you? Have you tried Pattern Matching? Share how it helps keep your code cleaner and clearer!
Data Engineer | Azure | AWS | Databricks | Snowflake | Apache Spark | Python | PySpark
3 周Informative post! Thanks for sharing, Cássio Huggentobler de Costa.
Senior Flutter Developer | iOS Developer | Mobile Developer | Flutter | Swift | UIKit | SwiftUI
3 周Very helpful Cássio Huggentobler de Costa! Thanks for sharing.
Fullstack Engineer | Software Developer | React | Next.js | TypeScript | Node.js | JavaScript | AWS
3 周Very informative
Senior Fullstack Software Developer | Java | Angular | React | Tech Lead
3 周Nice to see the Pattern Matching in your post of C#. I wrote this same topic and theme, but for Java. Are we synchronized ? kkkk Congratulations, really good article.
Manager Solutions Architecture | Machine Learning Engineer | Golang | Kotlin | Flutter | React Native | Angular | Figma | Java | .Net | Nodejs | DevOps | Maven | JUnit | CI/CD | GitHub | Design Patterns | Multi Cloud
3 周Very informative