-
Notifications
You must be signed in to change notification settings - Fork 396
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.
Either create your own or use From factory methods.
Length m = 1.Meters(); // No longer possible
Length m = Length.FromMeters(1); // Use this instead
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
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
To change the default culture used by ToString()
in quantities:
GlobalConfiguration.DefaultCulture = new CultureInfo("en-US"); // instead of UnitSystem.DefaultCulture
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);