Units of measure in C# - almost

后端 未结 12 1314
再見小時候
再見小時候 2020-11-27 10:29

Inspired by Units of Measure in F#, and despite asserting (here) that you couldn\'t do it in C#, I had an idea the other day which I\'ve been playing around with.

         


        
12条回答
  •  天命终不由人
    2020-11-27 11:12

    You could add extension methods on numeric types to generate measures. It'd feel a bit DSL-like:

    var mass = 1.Kilogram();
    var length = (1.2).Kilometres();
    

    It's not really .NET convention and might not be the most discoverable feature, so perhaps you'd add them in a devoted namespace for people who like them, as well as offering more conventional construction methods.

提交回复
热议问题