1What Is Pattern Matching in C#?
Hi friends! Today we will learn about Pattern Matching in modern C#. Pattern matching allows us to check a value against a particular condition or type and execute code based on the result.
Instead of writing multiple if-else conditions and explicit type checks, we can write cleaner and more readable code using patterns.
Modern C# provides features like type patterns, constant patterns, relational patterns, property patterns and switch expressions.
2Think About an Online Shopping App
Imagine an online shopping application. We receive orders with different payment statuses.
If an order is Pending, we show a waiting message. If it is Paid, we confirm the order. If the amount is above ₹5,000, we provide priority processing.
We can handle these conditions using pattern matching instead of writing lengthy if-else statements.
3Explore the Important Patterns
1. Constant Pattern
Checks whether a value matches a constant such as a number, string or enum value.
2. Type Pattern
Checks whether an object belongs to a particular type. It can also declare a variable of that type.
3. Relational Pattern
Checks conditions using operators such as greater than, less than and equal to.
4. Property Pattern
Checks an object's properties against specified values or conditions.
5. Switch Expression
Returns a value based on matching conditions. It provides a concise alternative to traditional switch statements.
| Pattern | Purpose |
|---|---|
| Constant | Match an exact value |
| Type | Match an object type |
| Relational | Compare numeric values |
| Property | Check object properties |
| Switch expression | Return a value based on patterns |
4Let's Build an Order Processing Program
Let's create an order class and use different patterns to identify the order status and priority.
using System;
class Order
{
public string Status { get; set; }
public double Amount { get; set; }
}
class Program
{
static string GetStatus(Order order) =>
order.Status switch
{
"Paid" => "Order confirmed",
"Pending" => "Waiting for payment",
_ => "Unknown status"
};
static void Main()
{
Order order = new Order
{
Status = "Paid",
Amount = 6000
};
Console.WriteLine(GetStatus(order));
string priority = order.Amount switch
{
> 5000 => "Priority processing",
> 0 => "Normal processing",
_ => "Invalid amount"
};
Console.WriteLine(priority);
object payment = "UPI";
if (payment is string method)
Console.WriteLine(method);
}
}
Here we use a switch expression for payment status, relational patterns for order amount and a type pattern to identify the payment method.
5Check the Application Result
Order confirmed Priority processing UPI
The order is Paid, so it is confirmed. The amount is greater than 5,000, so priority processing is selected. Finally, the type pattern identifies the payment value as a string.
6Watch Pattern Matching in VS Code
Follow how C# evaluates the order status, amount and payment type.
7What Should You Remember?
Pattern matching makes conditional logic cleaner and easier to maintain. It supports checking values, types, properties and ranges.
- Use constant and type patterns to match values and types.
- Use relational and property patterns for conditions.
- Use switch expressions to return values cleanly.
8Questions Interviewers Love to Ask
What is pattern matching in C#?
Answer: It checks whether a value matches a particular pattern, such as a type, constant, property or relational condition.
What is a type pattern?
Answer: A type pattern checks whether an object matches a specified type and can declare a variable of that type.
What is the difference between switch statement and switch expression?
Answer: A switch statement executes code for matching cases. A switch expression evaluates patterns and returns a value.
What are relational patterns?
Answer: They compare values using operators such as greater than, less than, greater than or equal to, and less than or equal to.
What is a property pattern?
Answer: It checks one or more properties of an object. For example, order is { Status: "Paid", Amount: > 5000 }.