Mike Chaliy
Mike Chaliy

Reputation: 26658

Shortest declaration of the read/write property in F#

I need a few public properties, in C# I will do this way.

public VendorOrderService { get; set; }

What is the shortest (correct/idiomatic) syntax for such properties in F#?

member val VendorService = Unchecked.defaultof<VendorOrderService> with get, set

P.S. I do understand that public properties are not super idiomatic for F#. But this code is working in larger .NET project, so such properties are obligatory.

Upvotes: 0

Views: 312

Answers (2)

Sergey Tihon
Sergey Tihon

Reputation: 12883

First of all in C# you should write type, like this

public string VendorOrderService { get; set; }

In F# 3.0 you can use val keyword (just like you are):

type MyType() = 
    member val VendorOrderService = "" with get, set

or use [CLIMutable] attribute:

[<CLIMutable>]
type MyType = { VendorOrderService:string}

Upvotes: 5

controlflow
controlflow

Reputation: 6737

type Foo() =
     member val Text : string = null with get, set

Upvotes: 2

Related Questions