F# Classes Declaring a class

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

// class declaration with a list of parameters for a primary constructor
type Car (model: string, plates: string, miles: int) =    

    // secondary constructor (it must call primary constructor)
    new (model, plates) = 
        let miles = 0
        new Car(model, plates, miles)
    
    // fields
    member this.model = model
    member this.plates = plates
    member this.miles = miles

Creating an instance

let myCar = Car ("Honda Civic", "blue", 23)
// or more explicitly
let (myCar : Car) = new Car ("Honda Civic", "blue", 23)


Got any F# Question?