C# switch expressions with property patterns for cleaner code

You are still writing five-level if/else blocks to handle types and conditions. C# 8 brought switch expressions with property patterns, and they have been sitting there ever since, quietly judging you. Instead of checking properties one by one, you match against the whole shape of an object in one clean expression. --- var price = order switch {   { Drink: "Espresso", ExtraShot: true } => 4.50,   { Drink: "Latte", Size: <= 12 }    => 3.75,   { Size: >= 20 }            => 6.00,   _                   => 2.50 }; --- Shipped in C# 8 (2019) and quietly upgraded every version since - relational patterns, nested patterns, you name it. Microsoft kept cooking on this one. Run it, break it, learn it: https://lnkd.in/eqSfeq5T #dotnet #csharp #programming #cleancode

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories