F# provides the feature
Units of Measure for sufficiently handling measurable entities of different units and quantities. Unfortunately this functionality is not accessible from the other .NET languages.
I have searched the internet for a similar solution in C#, but the approaches I have found so far have been relatively heavy-weight and not easily extendable. In particular, the measure type is generally implemented as a class with several members.
I would prefer a more light-weight solution where the measure type is implemented as a struct, preferably with a single member representing the measured amount in some reference unit for each specific quantity. At the same time I would like to have quantity type safety during compile-time, and it should the library should be easily extendable with new quantities and units without affecting the implementation of the measure type.
Based on these requirements, I have now implemented a compact C# Units of Measure library. I have implemented the library using the .NET 3.5 profile, and I have verified that it is possible to build under Mono (2.8 or later) as well. As far as I can tell, there is nothing preventing use of this library from other .NET languages either.
To facilitate feedback from other developers with similar requests, I have made the library available as open source (under the Eclipse Public License). The library is denoted
csunits and is available on
Github. A quick introduction to the API and instructions for extending the library are also provided on this web site.
I plan to continuously extend the library with new quantities and units, as well as add more functionality to it. In particular, I plan to implement a measure array type for efficient numerical handling of large number of measures in the same unit.
Any feedback on the library is more than welcome. Issues are preferably reported on the Github project web site, but comments to this blog post are perfectly OK as well :-) And ideas on how to further extend the library are of course very much appreciated.