DEV Community

wk
wk

Posted on

4 2

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.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay