我有一个扩展方法
type System.Int32 with
member this.Thousand() = this * 1000
Run Code Online (Sandbox Code Playgroud)
但它需要我这样写
(5).Thousand()
Run Code Online (Sandbox Code Playgroud)
我想摆脱两个括号,从使它成为一个属性而不是一个方法(为了学习的缘故)我如何使这个属性?
Jon的答案是一种方法,但对于只读属性,还有一种更简洁的方式来编写它:
type System.Int32 with
member this.Thousand = this * 1000
Run Code Online (Sandbox Code Playgroud)
此外,根据您的喜好,您可能会发现写入5 .Thousand(注意额外空间)比(5).Thousand(但您无法做到5.Thousand,甚至不能)更令人愉快5.ToString().