Units of measure in C# - almost

后端 未结 12 1282
再見小時候
再見小時候 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 10:58

    Using separate classes for different units of the same measure (e.g., cm, mm, and ft for Length) seems kind of weird. Based on the .NET Framework's DateTime and TimeSpan classes, I would expect something like this:

    Length  length       = Length.FromMillimeters(n1);
    decimal lengthInFeet = length.Feet;
    Length  length2      = length.AddFeet(n2);
    Length  length3      = length + Length.FromMeters(n3);
    

提交回复
热议问题