Pattern matching is a powerful feature in C# that simplifies how data and conditions are handled. This allows you to view the object's size, quality, or value in a concise and readable format. This feature, introduced in C# 7.0 and enhanced in later versions, replaces complex conditional logic with cleaner and more transparent code.
Pattern matching allows you to directly evaluate the size, type, or value of objects within the expression, without the need for a complex conditional statement. It's similar to what you might find in functional programming languages, but C# provides syntax and builds it.
Instead of using multiple "if-else" or "switch" blocks to check types and values, pattern matching provides a cleaner and more efficient way of performing these checks. It is most commonly used with "is" expressions and "switch" expressions/statements.
C# provides several types of pattern matching, including:
Type patterns are the simplest form of pattern matching. It checks if a value is of a certain type and assigns it to a new variable.
Here, In this example, the is operator checks the object type. If it matches string or int, the value is assigned to the variable s or i, and we can directly use it in the following block.
csharppublic void PrintType(object obj) { if (obj is string s) { Console.WriteLine($"The object is a string with value: {s}"); } else if (obj is int i) { Console.WriteLine($"The object is an integer with value: {i}"); } else { Console.WriteLine("Unknown object type."); } }
A constant pattern compares the input to a specific constant value, much like a traditional switch statement.
Here, in this example, the switch expression is used to match the character grade to constant values. The corresponding result is returned if the grade matches any of the specified letters.
csharppublic string EvaluateGrade(char grade) { return grade switch { 'A' => "Excellent", 'B' => "Good", 'C' => "Average", 'D' => "Below Average", 'F' => "Fail", _ => "Invalid grade" }; }
The relational patterns allow you to compare values using relational operators, like <, >, <=, or >=.
This example categorizes a person’s age based on relational comparisons. It uses relational operators within the switch expression, making the code compact and easier to understand.
csharppublic string CategorizeAge(int age) { return age switch { < 13 => "Child", >= 13 and < 18 => "Teenager", >= 18 and < 65 => "Adult", >= 65 => "Senior", _ => "Unknown age" }; }
The Logical patterns combine multiple patterns using logical operators like "and", "or", and "not".
Here, the and operator is used to combine two conditions. The switch statement is much clearer, and there is no need for additional nested if statements.
csharppublic string DescribeNumber(int number) { return number switch { > 0 and < 10 => "Single-digit positive number", >= 10 and < 100 => "Two-digit positive number", <= 0 => "Zero or negative number", _ => "Number is too large" }; }
Property patterns allow you to match object properties rather than the object itself. This pattern is especially useful when you want to test the state of an object.
In this example, the property pattern matches the Age property of the Person class. This makes it simple to classify a person based on their age without multiple condition checks.
csharppublic string DescribePerson(Person person) { return person switch { { Age: < 18 } => "Minor", { Age: >= 18, Age: < 65 } => "Adult", { Age: >= 65 } => "Senior", _ => "Unknown person" }; } public class Person { public string Name { get; set; } public int Age { get; set; } }
Positional patterns allow you to match the elements of a tuple or an object’s deconstructed form.
In this example, the positional pattern is used to match the x and y coordinates of a point and deconstruct a tuple to describe its position in a two-dimensional plane.
csharppublic string DescribePoint((int x, int y) point) { return point switch { (0, 0) => "Origin", (0, _) => "On the Y-axis", (_, 0) => "On the X-axis", _ => "Somewhere in the plane" }; }
The following are the benefits of pattern matching.
Copyrights © 2024 letsupdateskills All rights reserved