DimensionalQuantities 1.7.0
See the version list below for details.
dotnet add package DimensionalQuantities --version 1.7.0
NuGet\Install-Package DimensionalQuantities -Version 1.7.0
<PackageReference Include="DimensionalQuantities" Version="1.7.0" />
<PackageVersion Include="DimensionalQuantities" Version="1.7.0" />
<PackageReference Include="DimensionalQuantities" />
paket add DimensionalQuantities --version 1.7.0
#r "nuget: DimensionalQuantities, 1.7.0"
#:package DimensionalQuantities@1.7.0
#addin nuget:?package=DimensionalQuantities&version=1.7.0
#tool nuget:?package=DimensionalQuantities&version=1.7.0
DimensionalQuantities
A C# library for dimensional quantities, with generated type aliases, SI unit conversions, and compile-time dimensional arithmetic.
We developed this library for our own internal use and are sharing it because we believe it may be useful to the wider developer community. Issues and pull requests are welcome, but we make no support commitments or warranties, and we cannot guarantee that we will respond to issues or review or merge pull requests.
Usage
The runtime library targets .NET Standard 2.0. Consumers need a C# 14 compiler for the generated extension blocks. The DimensionalQuantities NuGet package includes both the runtime library and its source generator.
Voltage voltage = Voltage.FromVolts(12);
Current current = Current.FromAmps(3);
Resistance resistance = voltage / current;
Power power = voltage * current;
double ohms = resistance.AsOhms(); // 4
double watts = power.AsWatts(); // 36
SI prefixes and compound units
Named helpers handle conversion into and out of SI prefixes, including powers of prefixed units:
Length length = Length.FromMilliMetres(2500);
double metres = length.AsMetres(); // 2.5
Density density = Density.FromGramsPerCubicCentiMetre(1);
double kilogramsPerCubicMetre = density.AsSiBaseUnit(); // 1000
For more control, FromPrefixedUnits and AsPrefixedUnits accept decimal prefix exponents for each unit factor. Powers are taken into account automatically:
// 1 A/mm² = 1,000,000 A/m²
CurrentDensity density = CurrentDensity.FromPrefixedUnits(
1, ampPrefixExponent: 0, metrePrefixExponent: -3);
double ampsPerSquareMillimetre = density.AsPrefixedUnits(
ampPrefixExponent: 0, metrePrefixExponent: -3); // 1
Named compound units remain a single factor where appropriate: Voltage.FromPrefixedUnits(2, -3) means 2 millivolts, without requiring you to know the SI dimensions of a volt.
Arithmetic and generated intermediate quantities
The generator derives dimensions through arithmetic and generates the required operators and intermediate aliases. Quantities also support addition, subtraction and equality checks with quantities of the same dimension:
Current current = Current.FromAmps(3);
Resistance resistance = Resistance.FromOhms(4);
CurrentSquared squared = current * current;
Power heating = current * current * resistance; // 36 W
Current doubled = 2 * current;
Current halved = current / 2;
Current total = current + doubled;
double ratio = doubled / current; // 2: matching dimensions cancel
Frequency frequency = 1 / Time.FromSeconds(0.02); // 50 Hz
Known dimensions use names from the engineering catalogue, such as Power, Voltage, Permeability and MagneticFluxDensity. Other dimensions receive mechanical names such as CurrentSquared or TimePerCurrent. Discovered quantities have SI base-unit and generic prefix conversion helpers.
Aliases with the same dimensions share a type. For example, Energy and Torque are interchangeable: the library checks dimensions, not the physical meaning of a calculation. Magnitudes are stored as double in coherent SI base units.
Powers and roots
SI base and catalogue quantities have Squared(), Cubed(), Pow4() through Pow6(), and the corresponding SquareRoot(), CubeRoot(), Root4() through Root6() helpers. Helpers for intermediate quantities are generated when called.
For other integer or rational powers, use Power<TExponent>.Of(...). English exponent aliases such as NumberTwo, NumberHalf and NumberTwoOverThree are generated on demand:
using DimensionalQuantities.TypeSystem;
Length length = Length.FromMetres(3);
Area area = length.Squared();
Volume volume = length.Cubed();
Length recovered = Power<NumberHalf>.Of(area); // 3 m
Fractional dimensions are supported. Results whose dimensions cancel completely, including a zeroth power, are returned as double.
Discovery through your code
Intermediate quantities can flow through:
varlocals and explicitly typed locals.- Conditional (
?:) and switch expressions whose value branches have matching dimensions. - Fields, properties and helper method returns, including declarations in other source files.
- Parameters, arrays, generic collections and indexers.
Canonical compound names can also be used directly in type declarations, without first writing an expression that produces them:
static LengthCurrent Combine(Length length, Current current) => length * current;
LengthCurrent combined = Combine(Length.FromMetres(2), Current.FromAmps(3));
var timed = combined * Time.FromSeconds(4);
double value = timed.AsSiBaseUnit(); // 24
The generator recognises canonical names such as LengthCurrent, LengthPerTime and SquareRootLength. Canonical-name discovery leaves existing bound types and explicit aliases alone. Incompatible dimensions remain compiler errors.
Sharing quantities between packages
Generated names are aliases for types in the shared runtime library. Independently compiled packages can therefore exchange the same quantity through method parameters, return values, collections and containing objects.
A consuming project with the generator can discover quantities from referenced values, generate local conversion helpers, and use those values in further arithmetic. Quantity aliases are generated separately in each compilation; they are not exported by a referenced assembly.
Current scope
Discovery follows supported expressions and declarations; it is not general control-flow or data-flow analysis. For example, it does not infer unresolved quantity results through async methods, iterator bodies or arbitrary reassignment chains. Dedicated analyser diagnostics and IDE fixes are still follow-up work.
The library is under active development; the API and supported expression patterns are still evolving.
Licence
MIT, copyright GoSim.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages (4)
Showing the top 4 NuGet packages that depend on DimensionalQuantities:
| Package | Downloads |
|---|---|
|
DimensionalQuantities.Extensions.CgsEmag
Named CGS electromagnetic and mechanical conversions for dimensional quantities. |
|
|
DimensionalQuantities.Extensions.Imperial
Named Imperial and US customary conversions for dimensional quantities. |
|
|
DimensionalQuantities.Extensions.Metric
Named metric conversions for dimensional quantities. |
|
|
DimensionalQuantities.Extensions.Nautical
Named nautical conversions for dimensional quantities. |
GitHub repositories
This package is not used by any popular GitHub repositories.