in Dot Net by (243 points)
Property patterns IN c#.net

1 Answer

0 votes
by (243 points)

The property pattern enables you to match on properties of the object examined. Consider an eCommerce site that must compute sales tax based on the buyer's address. That computation isn't a core responsibility of an Address class. It will change over time, likely more often than address format changes. The amount of sales tax depends on the State property of the address. The following method uses the property pattern to compute the sales tax from the address and the price:

public static decimal ComputeSalesTax(Address location, decimal salePrice) =>

    location switch

    {

        { State: "WA" } => salePrice * 0.06M,

        { State: "MN" } => salePrice * 0.75M,

        { State: "MI" } => salePrice * 0.05M,

        // other cases removed for brevity...

        _ => 0M

    };

Related questions

0 votes
asked Dec 8, 2019 in Dot Net by Sinaya (243 points)
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya (243 points)
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya (243 points)
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya (243 points)
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya (243 points)
+1 vote
0 votes
asked Sep 23 in Dot Net by Robin (14.6k points)
0 votes
asked Sep 21 in Dot Net by Robin (14.6k points)
0 votes
asked Sep 21 in Dot Net by Robin (14.6k points)
...