Skip to content

Getting Started

Marcus Ackre Medina edited this page Jul 20, 2026 · 1 revision

Getting Started

Install

dotnet add package MarcusMedina.Units.Weight

Requirements: .NET 10.0+, C# 14.0+

Philosophy

Weight is a readonly struct that always stores its value internally as grams. Every unit family (Metric, US, British, BritishOld, SwedishOld) lives in its own namespace and adds two kinds of extension methods:

  • A creation extension on int/double, e.g. 11.Stone() or 2.5.Kilograms(), which produces a Weight.
  • A conversion extension on Weight, e.g. weight.ToPounds() or weight.ToSkålpund(), which returns a plain double.

Because the underlying storage is always grams, you can freely mix units from different systems in the same expression — 2.5.Kilograms() + 1.Pounds() just works, no manual conversion required. You only need to using the namespace(s) for the units you actually want to create or read; the Weight struct itself lives in the root MarcusMedina.Units.Weight namespace.

Arithmetic (+, -, *, /) and full comparison operators (<, >, ==, etc.) are supported directly on Weight values.

First example

using MarcusMedina.Units.Weight.Metric;
using MarcusMedina.Units.Weight.British;

// Create weights from different unit systems
Weight parcel = 2.5.Kilograms();
Weight person = 11.Stone();

// Convert to whichever unit you need to display
double parcelInPounds = parcel.ToPounds();   // ≈ 5.51
double personInKg     = person.ToKilograms(); // ≈ 69.85

// Mixing units in arithmetic is always safe —
// Weight is stored internally in grams regardless of the unit used to create it
Weight combined = parcel + 1.Pounds();

// Comparisons work directly on Weight values
bool personIsHeavier = person > parcel;

Next: see the API Reference for the full list of supported units across all five unit families.

Clone this wiki locally