This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
UnitsNet is a .NET library that provides strongly-typed physical units and quantities, enabling safe and intuitive unit conversions in code. The library uses code generation from JSON definitions to create type-safe APIs for over 130 physical quantities.
- Build project:
build.batordotnet build UnitsNet.slnx - Build all targets (including nanoFramework):
build-all-targets.bat - Run tests:
test.batordotnet test UnitsNet.slnx - Run single test:
dotnet test UnitsNet.Tests --filter "FullyQualifiedName~TestClassName.TestMethodName" - Clean artifacts:
clean.bat
- Generate code from JSON definitions:
generate-code.batordotnet run --project CodeGen- Always run this after modifying any JSON files in
Common/UnitDefinitions/ - The generator reads 131 JSON definition files and creates C# code
- Always run this after modifying any JSON files in
- Modify unit definitions in
Common/UnitDefinitions/*.json - Run
generate-code.batto regenerate C# code - Run
build.batto compile and test - Use
test.batfor isolated test runs
- UnitsNet/: Main library with quantity types and units
GeneratedCode/: Auto-generated from JSON definitions (do not edit manually)CustomCode/: Hand-written code extending generated types
- UnitsNet.Tests/: Comprehensive test suite
- CodeGen/: Code generation tool that creates C# from JSON definitions
- Common/UnitDefinitions/: 131 JSON files defining physical quantities
- UnitsNet.NumberExtensions/: Extension methods for numeric types
- UnitsNet.Serialization.*/: JSON.NET and System.Text.Json serialization support
- UnitsNet.NanoFramework/: Support for embedded .NET nanoFramework
The project uses a sophisticated code generation system:
- JSON definitions in
Common/UnitDefinitions/describe units, conversions, and localizations CodeGenproject processes these to generate:- Quantity types (e.g.,
Length,Mass) - Unit enums (e.g.,
LengthUnit,MassUnit) - Conversion logic and unit abbreviations
- Quantity types (e.g.,
- Generated code goes to
*/GeneratedCode/folders - Custom code in
*/CustomCode/extends generated types
- IQuantity: Base interface for all quantity types
- Quantity: Static class for dynamic quantity operations
- UnitConverter: Handles conversions between units
- QuantityParser/UnitParser: Parse strings to quantities/units
- UnitsNetSetup: Configuration singleton
- Edit or create JSON file in
Common/UnitDefinitions/ - Follow conversion function guidelines:
- Use multiplication for
FromUnitToBaseFunc - Use division for
FromBaseToUnitFunc - Prefer scientific notation (1e3, 1e-5)
- Use multiplication for
- Run
generate-code.bat - Add tests if needed
- Follow
.editorconfigspecifications - Use ReSharper settings in
UnitsNet.sln.DotSettings - Treat warnings as errors (except obsolete warnings)
- Add file headers to new files
- Base units are chosen for each quantity (e.g., meter for Length)
- All conversions go through the base unit
- Use superscript in abbreviations: cm², m³
- Compound units format: N·m (dot), km/h (slash)
- Test class naming:
<Type>Tests - Test method naming:
<method>_<condition>_<result> - Tests accept error margin of 1E-5 for most units
- Separate projects for nanoFramework compatibility
- Use
build-all-targets.batto include nanoFramework builds - Limited feature set compared to full .NET
- Conversion functions are compiled to delegates for performance
- All conversions go through base units (potential for small errors)
- Precision goal is 1E-5 for most units
- Unit abbreviations support multiple cultures
- JSON definitions include translations for various languages
- Default culture: Thread.CurrentCulture, fallback to en-US
- Quantity types:
UnitsNet/GeneratedCode/Quantities/*.g.cs - Unit enums:
UnitsNet/GeneratedCode/Units/*.g.cs - Custom extensions:
UnitsNet/CustomCode/Quantities/*.extra.cs - Unit definitions:
Common/UnitDefinitions/*.json
- Generator entry:
CodeGen/Program.cs - Generator logic:
CodeGen/Generators/ - Enable verbose logging: Check Serilog configuration in Program.cs
- Execute:
dotnet run -c Release --project UnitsNet.Benchmark - Results saved to
Artifacts/folder
See .claude/criteria-for-adding-quantities-and-units.md for instructions on adding new quantities or units to ensure they are widely used and well defined.