You do not need dimensional analysis. For example (from the answer you referenced) in F # you can do this:
let g = 9.8<m/s^2>
and it will generate a new acceleration unit derived from meters and seconds (you can actually do the same in C ++ using templates).
In C #, you can perform dimensional analysis at runtime, but it adds extra overhead and prevents you from checking compile time. As far as I know, there is no way to do full compile time units in C #.
Whether it is worth doing, of course, depends on the application, but for many scientific applications this is definitely a good idea. I do not know of any existing libraries for .NET, but they probably exist.
If you're curious about how to do this at runtime, the idea is that each value has a scalar value and integers representing the power of each base block.
class Unit { double scalar; int kg; int m; int s; // ... for each basic unit public Unit(double scalar, int kg, int m, int s) { this.scalar = scalar; this.kg = kg; this.m = m; this.s = s; ... } // For addition/subtraction, exponents must match public static Unit operator +(Unit first, Unit second) { if (UnitsAreCompatible(first, second)) { return new Unit( first.scalar + second.scalar, first.kg, first.m, first.s, ... ); } else { throw new Exception("Units must match for addition"); } } // For multiplication/division, add/subtract the exponents public static Unit operator *(Unit first, Unit second) { return new Unit( first.scalar * second.scalar, first.kg + second.kg, first.m + second.m, first.s + second.s, ... ); } public static bool UnitsAreCompatible(Unit first, Unit second) { return first.kg == second.kg && first.m == second.m && first.s == second.s ...; } }
If you do not allow the user to change the value of units (a good idea anyway), you can add subclasses for common units:
class Speed : Unit { public Speed(double x) : base(x, 0, 1, -1, ...);
You can also define more specific operators on derived types to avoid checking for compatible units on generic types.