Build with an Azure free account. Get USD200 credit for 30 days and 12 months of free services.

Start free today

C# Language Highlights: Positional Pattern Matching

Play C# Language Highlights: Positional Pattern Matching

The Discussion

  • User profile image
    olivier
    Using the new relational patterns, you could do it without the `when` clauses. Also, the default case is not necessary, as the previous cases are exhaustive. Especially, the `(_, _)` case catches all the remaining cases. It could also be replaced by a simple default case.

    static Quadrant GetQuadrant(Point point) => point switch {
    (0, 0) => Quadrant.Origin,
    ( > 0, > 0) => Quadrant.One,
    ( < 0, > 0) => Quadrant.Two,
    ( < 0, < 0) => Quadrant.Three,
    ( > 0, < 0) => Quadrant.Four,
    (_, _) => Quadrant.OnBorder
    };

    If the point was nullable this switch expression would not be exhaustive, and the default case would have to be re-introduced.

Add Your 2 Cents