🔟 Real Problems You Can Fix with C# Pattern Matching
davinceleecode

davinceleecode @davinceleecode

About: Software dev with experience in Windows, .NET, web dev, APIs, .NET Core, Razor Pages, and ReactJS. I love exploring new tech and building modern solutions.

Location:
Void
Joined:
Mar 8, 2025

🔟 Real Problems You Can Fix with C# Pattern Matching

Publish Date: May 30
10 3

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;
}
Enter fullscreen mode Exit fullscreen mode

New Way (Type Pattern - C# 7.0):

if (obj is Customer customer)
{
    // No need for explicit type casting
}
Enter fullscreen mode Exit fullscreen mode

2 🔄 switch Case Logic
Old Way:

switch (fruit)
{
    case "Apple":
        Console.WriteLine("Red");
        break;
    case "Banana":
        Console.WriteLine("Yellow");
        break;
}

Enter fullscreen mode Exit fullscreen mode

New Way (Switch Expression):

var color = fruit switch
{
    "Apple" => "Red",
    "Banana" => "Yellow",
    _ => "Unknown"
};
Enter fullscreen mode Exit fullscreen mode

3 🔢 Enum Value Checks
Old Way:

if (status == Status.Active || status == Status.Pending)
{
    // do something
}
Enter fullscreen mode Exit fullscreen mode

New Way:

if (status is Status.Active or Status.Pending)
{
    // cleaner
}
Enter fullscreen mode Exit fullscreen mode

4 🔁 Nested if-else Blocks
Old Way:

if (obj is Employee)
{
    var emp = (Employee)obj;
    if (emp.Department == "HR")
    {
        Console.WriteLine("HR Employee");
    }
}
Enter fullscreen mode Exit fullscreen mode

New Way:

if (obj is Employee { Department: "HR" })
{
    Console.WriteLine("HR Employee");
}
Enter fullscreen mode Exit fullscreen mode

5 🧹 Multiple Property Conditions Causing Clutter
Old Way:

if (customer != null && customer.Age > 18 && customer.Location == "US")
{
    Console.WriteLine("Valid customer");
}
Enter fullscreen mode Exit fullscreen mode

New Way:

if (customer is { Age: > 18, Location: "US" })
{
    Console.WriteLine("Valid customer");
}
Enter fullscreen mode Exit fullscreen mode

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...");
}
Enter fullscreen mode Exit fullscreen mode

New Way:

var commandResult = command switch
{
    "Start" or "Run" or "Begin" => "Executing action...",
    "Stop" or "End" or "Terminate" => "Stopping action...",
    _ => "Unknown command"
};
Enter fullscreen mode Exit fullscreen mode

7 🔢 Complex Number Range Checks
Old Way:

if (score >= 50 && score <= 100)
{
    Console.WriteLine("Valid score range");
}
Enter fullscreen mode Exit fullscreen mode

New Way:

if (score is >= 50 and <= 100)
{
    Console.WriteLine("Valid score range");
}
Enter fullscreen mode Exit fullscreen mode

8 🧮 Verbose Tuple Checks
Old Way:

if (coordinates.X == 10 && coordinates.Y == 20)
{
    Console.WriteLine("Match found");
}
Enter fullscreen mode Exit fullscreen mode

New Way:

if (coordinates is (10, 20))
{
    Console.WriteLine("Match found");
}

Enter fullscreen mode Exit fullscreen mode

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.");
}
Enter fullscreen mode Exit fullscreen mode

New Way:

var result = (isActive, isVerified) switch
{
    (true, true) => "User has access.",
    (false, _) or (_, false) => "Access denied."
};

Console.WriteLine(result);
Enter fullscreen mode Exit fullscreen mode

10 🔁 Checking for Multiple Values in an Array
Old Way:

int[] validValues = { 10, 20, 30 };
if (validValues.Contains(value))
{
    Console.WriteLine("Value is valid");
}
Enter fullscreen mode Exit fullscreen mode

New Way (List Pattern Matching - C# 11+):

if (validValues is [_, >= 20, 30])
{
    Console.WriteLine("Value is valid");
}

Enter fullscreen mode Exit fullscreen mode

If you found this helpful, consider supporting my work at ☕ Buy Me a Coffee.

Comments 3 total

  • Shifa
    ShifaMay 30, 2025

    such a great article

  • AliceAtkins
    AliceAtkinsJun 3, 2025

    I had been looking for a convenient betting platform for a long time and stumbled upon 1win South Africa by accident. I was pleasantly surprised by the simple registration process—I just went to the website and quickly completed the 1win register online. The interface is intuitive, there are many events, and the odds are excellent. I have already withdrawn my winnings several times without any delays. I recommend it!

Add comment