This console app plans a day for an electric delivery van using mixed-unit route inputs, a parcel manifest, driving estimates, and a charging stop. The generated quantity structs and unit enums become part of this app at build time; no generated C# is checked in. Choose its dependency source with the solution platform described in the samples overview.
The scenario is split into readable sections that exercise:
- parsing and defensive
TryParseat input boundaries; - numeric conversion,
As,ToUnit, and formattedToStringoutput; - arithmetic and collection aggregation across mixed units;
- generated
Length / Duration = SpeedandPower * Duration = Energyrelationships; - an application-specific
ParcelCountgenerated from JSON; - the selected-module registry and the legacy-shaped
Quantityfacade; - explicit immutable
UnitSystempolicy and generated System.Text.Json support.
Open the samples workspace as described in the samples overview, choose
Sample: Modular playground in Run and Debug, then press F5 to debug or Ctrl+F5 to run
without the debugger. Output appears in the integrated terminal.
You can also open a terminal and run:
dotnet runEvery edit uses the usual .NET feedback loop: save a file and run dotnet run again.
When using Debug | LocalPackages in an IDE, run the sample refresh script to create uniquely
versioned packages from the current checkout and force every sample to restore that exact version:
pwsh ../refresh-local-packages.ps1The unique version avoids reusing older packages from NuGet's global package cache. The script
packs the library and sample definition package in Release, then restores all samples for
Debug | LocalPackages; pass -Configuration Release to restore the release configuration instead.
Start with any of these:
- In
ApplicationUnits.cs, add or remove a built-inIInclude<...>quantity selection. - Change a
[UnitSet]list and see which enum members remain available after rebuilding. - In
ParcelCount.unitsnet.json, add a unit or change a conversion expression. - In
Program.cs, use the generated types, conversions, parsing, formatting, or operators.
The project enables EmitCompilerGeneratedFiles, so after a build you can also inspect the emitted
C# under obj/<dependency-platform>/<configuration>/<framework>/Generated/UnitsNet.Modular.Generator/UnitsNet.Modular.Generator.UnitsNetModularGenerator/.
Delete or ignore that folder when you are done; it is build output and is not part of the project.
For the complete authoring model, see the UnitsNet.Modular documentation.