in Dot Net by
Property patterns IN

▼ Show 1 Answer

0 votes

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


Learn More with Madanswer

Related questions

0 votes
asked Dec 8, 2019 in Dot Net by Sinaya
0 votes
asked Dec 8, 2019 in Dot Net by Sinaya
0 votes
0 votes
+1 vote
asked Jun 25, 2019 in Dot Net by Venkatshastri