Skip to content

Upgrading from 3.x to 4.x

Andreas Gullberg Larsen edited this page Nov 8, 2018 · 13 revisions

These are the usecases we expect most consumers to have to deal with when upgrading.

Number extension methods removed

Either create your own or use From factory methods.

Length m = 1.Meters(); // No longer possible
Length m = Length.FromMeters(1); // Use this instead

UnitSystem abbreviations => UnitAbbreviationsCache

For looking up or mapping custom unit abbreviations, instead of UnitSystem use UnitAbbreviationsCache.

var abbreviations = UnitAbbreviationsCache.Default;
abbreviations.GetAllUnitAbbreviationsForQuantity(typeof(LengthUnit)); // ["cm", "dm",...]
abbreviations.GetUnitAbbreviations(typeof(LengthUnit), 6); // ["ft", "'", "`"]
abbreviations.GetDefaultAbbreviation(typeof(LengthUnit), 1); // "cm"
abbreviations.MapUnitToAbbreviation(typeof(LengthUnit), 1, new CultureInfo("en-US"), "foo"); // add custom abbreviation for centimeter

UnitSystem parsing => UnitParser

All parsing of unit abbreviations are now done by UnitParser:

var parser = UnitParser.Default;
parser.Parse("cm", typeof(LengthUnit)); // 1
parser.TryParse("cm", typeof(LengthUnit), out object val); // returns true, val = 1

UnitSystem.DefaultCulture => GlobalConfiguration.DefaultCulture

To change the default culture used by ToString() in quantities:

GlobalConfiguration.DefaultCulture = new CultureInfo("en-US"); // instead of UnitSystem.DefaultCulture

Nullable From-methods removed

int? val = null;

// Instead of this
Length? l = Length.FromMeters(val);

// Do this, or create your own convenience factory method
Length? l = val == null ? (Length?)null : Length.FromMeters(val.Value);
Clone this wiki locally