DEV Community

wk
wk

Posted on

Pattern matching F# vs C#

F#

type Address =
    { State: String }

let computeSalesTax address salePrice =
    match address with
    | { State = "WA" } -> salePrice * 0.06m
    | { State = "MN" } -> salePrice * 0.75m
    | { State = "MI" } -> salePrice * 0.05m
    | _ -> 0m

C#

class Address {
    public string State { set; get; }
}


static decimal ComputeSalesTax(Address address, decimal salePrice) =>
    address switch
    {
        { State: "WA" } => salePrice * 0.06M,
        { State: "MN" } => salePrice * 0.75M,
        { State: "MI" } => salePrice * 0.05M,
        _ => 0M
    }

Top comments (2)

Collapse
 
saint4eva profile image
saint4eva

I love pattern matching a lot. And I love C#.

Collapse
 
mburszley profile image
Maximilian Burszley

Woo, 75% sales tax.