This content originally appeared on DEV Community and was authored by davinceleecode
C# pattern matching (introduced in C# 7.0 and enhanced in later versions) simplifies many everyday coding problems. Here are 10 real-world examples where it makes your code cleaner, safer, and more readable.
1 ❌ The Classic Null Check Issue
Old Way:
if (obj != null && obj is Customer)
{
var customer = (Customer)obj;
}
New Way (Type Pattern - C# 7.0):
if (obj is Customer customer)
{
// No need for explicit type casting
}
2 🔄 switch Case Logic
Old Way:
switch (fruit)
{
case "Apple":
Console.WriteLine("Red");
break;
case "Banana":
Console.WriteLine("Yellow");
break;
}
New Way (Switch Expression):
var color = fruit switch
{
"Apple" => "Red",
"Banana" => "Yellow",
_ => "Unknown"
};
3 🔢 Enum Value Checks
Old Way:
if (status == Status.Active || status == Status.Pending)
{
// do something
}
New Way:
if (status is Status.Active or Status.Pending)
{
// cleaner
}
4 🔁 Nested if-else Blocks
Old Way:
if (obj is Employee)
{
var emp = (Employee)obj;
if (emp.Department == "HR")
{
Console.WriteLine("HR Employee");
}
}
New Way:
if (obj is Employee { Department: "HR" })
{
Console.WriteLine("HR Employee");
}
5 🧹 Multiple Property Conditions Causing Clutter
Old Way:
if (customer != null && customer.Age > 18 && customer.Location == "US")
{
Console.WriteLine("Valid customer");
}
New Way:
if (customer is { Age: > 18, Location: "US" })
{
Console.WriteLine("Valid customer");
}
6 🔤 Overcomplicated String Pattern Matching
if (command == "Start" || command == "Run" || command == "Begin")
{
Console.WriteLine("Executing action...");
}
else if (command == "Stop" || command == "End" || command == "Terminate")
{
Console.WriteLine("Stopping action...");
}
New Way:
var commandResult = command switch
{
"Start" or "Run" or "Begin" => "Executing action...",
"Stop" or "End" or "Terminate" => "Stopping action...",
_ => "Unknown command"
};
7 🔢 Complex Number Range Checks
Old Way:
if (score >= 50 && score <= 100)
{
Console.WriteLine("Valid score range");
}
New Way:
if (score is >= 50 and <= 100)
{
Console.WriteLine("Valid score range");
}
8 🧮 Verbose Tuple Checks
Old Way:
if (coordinates.X == 10 && coordinates.Y == 20)
{
Console.WriteLine("Match found");
}
New Way:
if (coordinates is (10, 20))
{
Console.WriteLine("Match found");
}
9 ✅ Boolean Flag Checks Requiring Extra Logic
Old Way:
if (isActive && isVerified)
{
Console.WriteLine("User has access.");
}
else if (!isActive || !isVerified)
{
Console.WriteLine("Access denied.");
}
New Way:
var result = (isActive, isVerified) switch
{
(true, true) => "User has access.",
(false, _) or (_, false) => "Access denied."
};
Console.WriteLine(result);
10 🔁 Checking for Multiple Values in an Array
Old Way:
int[] validValues = { 10, 20, 30 };
if (validValues.Contains(value))
{
Console.WriteLine("Value is valid");
}
New Way (List Pattern Matching - C# 11+):
if (validValues is [_, >= 20, 30])
{
Console.WriteLine("Value is valid");
}
If you found this helpful, consider supporting my work at ☕ Buy Me a Coffee.
This content originally appeared on DEV Community and was authored by davinceleecode

davinceleecode | Sciencx (2025-05-30T05:18:12+00:00) 🔟 Real Problems You Can Fix with C# Pattern Matching. Retrieved from https://www.scien.cx/2025/05/30/%f0%9f%94%9f-real-problems-you-can-fix-with-c-pattern-matching/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.