pattern matching in C#

Pattern Matching in C# 8

C# 7 introduced pattern matching with the extension of the switch statement and the is operator offering the const pattern, the type pattern, and the var pattern. With C# 8 an extension of pattern matching is planned, including the property pattern, the recursive pattern, and a new switch – the switch expression.

With C# 7, pattern matching was introduced in C#. The following sample makes use of pattern matching in the switch statement, and type pattern matches. With the first case, also the when clause is used to filter only shapes where the size of the shape has a minimum height. The second and third cases match shapes with a smaller height, but only objects of type Ellipse and Rectangle. For all the other shapes, the default case is chosen:

static string M1(Shape shape) {

    switch (shape) {

             case Shape s when s.Size.height > 100:

                     return $”large shape with size {s.Size} at position {s.Position}”;               case Ellipse e:

                    return $”Ellipse with size {e.Size} at position {e.Position}”;

             case Rectangle r:

                   return $”Rectangle with size {r.Size} at position {r.Position}”;

           default:

                  return “another shape”;

       }

}

Using the Switch Expression

C# 7 extended the scenarios where you can use expression bodied members. However, as soon as you use the switch statement, the method cannot be implemented using the expression syntax. This changes with the new switch expression.

The switch expression is simplified compared to the switch statement. First, the order of the switch keyword and the variable used is reversed. Instead of writing switch (shape), you write shape switch. The case keyword is not needed with the new syntax. Every case is decided by a pattern, e.g. the type pattern Ellipse e where the variable e is filled in this case. You can also use the when filter as used in the earlier C# 7 pattern matching sample. This syntax is the same as before. The break keyword is also not needed. The implementation of the case follows the lambda operator. After a comma, the next pattern specifies the next case. The new discard pattern with the _ specifies the default case. This new pattern can also be used with the switch statement used previously in case of default.

What if multiple statements are needed in a single case? You can use local functions in such a scenario.

static string M2(Shape shape) => shape switch

{

Shape s when s.Size.height > 100 => $”large shape with size {s.Size} at position {s.Position}”,

Ellipse e => $”Ellipse with size {e.Size} at position {e.Position}”,

Rectangle r => $”Rectangle with size {r.Size} at position {r.Position}”,

_ => “another shape”

};

C# 7 introduced pattern matching with the type pattern, the const pattern, and the var pattern. C# 8 extends pattern matching with the discard pattern, the property pattern, and the recursive pattern. Patterns that can simplify code written today.

The new switch expression offers a more modern way for switch/case/break, whereas the case and break keywords are no longer needed.

Thanks for dropping by !!! Feel free to comment to this post or you can drop me an email at naik899@gmail.com

Leave a Reply