// 可以这样: typeBoy1= valmutable _name :string valmutable _age :int member t.Name with get() = t._name and set(v) = t._name <- v member t.Age with get() = t._age and set(v) = t._age <- v new () = { _name = "" _age = 0 } // 这种形式要求必须实例化之后才能使用 let boya = let boy = new Boy1() boy._name <- "colder" boy._age <- Int32.MaxValue boy // 或 let boyb = new Boy1(Name = "colder", Age = Int32.MaxValue)
// 还可以定义成这样: typeBoy2(name :string, age :int) = letmutable _name = name letmutable _age = age member t.Name with get() = _name and set(v) = _name <- v member t.Age with get() = _age and set(v) = _age <- v // 实例化时参数即为字段 let boyc = new Boy2("colder", Int32.MaxValue)
// 这两种方式定义的类 都是在模仿 C# 定义类的方式 // 不过怎么看都没有 C#3.0 使用的 { get; set; } 形式简单 // 可不可以使用 Records 呢? // 答案是肯定的 生成的客户端代码并没有区别 typeBoy= { mutable Name :string mutable Age :int } // 实例化也是最简单的 let boyd = { Name = "colder" Age = Int32.MaxValue }
// 给 Boy 记录加上相关的属性 [<DataContract(Name="Boy")>] typeBoy= { [<DataMember(Order = 1)>] mutable Name :string [<DataMember(Order = 2)>] mutable Age :int } // 返回值采用标准的 List<T> 泛型 // 也就是微软官方视频教程中提到的 LINQ 表达式的 ToList() 方法返回的类型 [<ServiceContract(Name = "Wcf")>] typeWcf() = let boy = { Name = "colder" Age = Int32.MaxValue } [<OperationContract>] member t.BoyListT() = let list = new System.Collections.Generic.List<Boy>() list.Add boy list