Reputation: 47904
Is there a difference between using a read-only property:
type T(arg) =
member x.M = arg
and using an automatically implemented property:
type T(arg) =
member val M = arg
assuming arg
has no side effects? Any reason to prefer one over the other?
Upvotes: 7
Views: 1018
Reputation: 7735
The essential difference between those is that member val
represents an expression that is computed only once during instance initialization. Therefore,
type Person(fname, lname) =
member val Name = fname + lname // would be calculated once
So, the first consideration is performance.
Another consideration is based on two limitations of auto properties:
virtual
Upvotes: 12