Initializing a member variable (List<int>)

I’m trying to create a class with a list of int as member variable. However, it is not working as expected. See this example:

open System.Collections.Generic
type Class() =
member this.x = List()
member this.y = 1

let c = Class()
printfn "%d" c.y
printfn "%d" c.x.[0]

This is printing 1 and then throws ArgumentOutOfRangeException because the list is empty.
Would be grateful for an explanation :slight_smile:


This gets a new empty list, and appends 1 to it.

printfn "%d" c.x.[0]

This then gets a new empty list, and tries to look up the first element, which doesn’t exist.

Ok so if I understand you correct, Class.x becomes a property with that syntax. Thanks!