QuantityValue implemented as a fractional number 🐲
QuantityValueimplemented as a fractional numberIQuantityinterfaces optimized (some methods refactored as extensions)UnitInfo: introduced two new properties:ConversionFromBaseandConversionToBasewhich are used instead of theswitch(Unit)conversionUnitsNetSetup: introduced helper methods for adding external quantities, or re-configuring one or more of the existing onesUntAbbreviationsCache: introduced additional factory methods (using a configuration delegate)UnitParser: introduced additional factory methods (using a configuration delegate)UnitConverter: re-implemented (multiple versions)Inverserelationship mapping implemented as a type of implicit conversion- updated the JsonNet converters
- introducing the SystemTextJson project
- added a new UnitsNetConfiguration project to the Samples, showcasing the new configuration options
- many more tests and benchmarks (perhaps too many)
@angularsen Clearly, I don't expect this to get merged in the Gitty up! fashion, but at least we have the whole picture, with sources that I can reference.
If you want, send me an e-mail, we could do a quick walk-through / discussion.
100k lines removed, 100k lines added 🙈
100k lines removed, 100k lines added 🙈
I tried to create this PR twice before (many months ago), while the changes to the unit definitions were still not merged- and the web interface was giving me an error when trying to browse the files changed.. Something like "Too many files to display" 😄
Ok, I'm not going to get through a review of this many files anytime soon. Maybe we should have a screen sharing session and go through it together. I may have some time this weekend, what about you? What timezone are you in?
On the surface though, it seems like this could be split up into chunks. I know it's tedious and extra work, but it will be way faster to review. Do you see any chunks of changes to easily split off into separate PRs?
Ok, I'm not going to get through a review of this many files anytime soon. Maybe we should have a screen sharing session and go through it together. I may have some time this weekend, what about you? What timezone are you in?
Sofia (GMT+3), but time zones are not relevant to my sleep schedule - so basically any time you want.
On the surface though, it seems like this could be split up into chunks. I know it's tedious and extra work, but it will be way faster to review. Do you see any chunks of changes to easily split off into separate PRs?
Yes, I do have some ideas:
- [x]
UnitAbbreviationsCacheand theUnitParsershould be more or less free of changes once #1548 is merged - [x] I plan to remove the
IConvertibleinterface tonight (lots of red points there) - [x] The
QuantityParserhas just a few minor changes which I was going to try to push as well (other than that it's mostly justdoublechanging toQuantityValue) - [x]
QuantityFormatter- there was an issue that I created earlier that should (mostly) solve the differences - [x] Refactoring the
QuantityInfocan theoretically be done without theConversionExpressions (which would open the way for the changes to theIQuantityinterface and some of the extension methods). - [x]
UnitParser: introduce two new method:GetUnitFromAbbreviationandTryGetUnitFromAbbreviationreturning aUnitInfo(which could be used for constructing an instance of the quantity) - [x] Introduce the changes to the
IQuantityinterface and some of the extension methods - [x] Move the exceptions in their own folder and replace the usages of the
NotImplementedExceptionwith the appropriateUnitNotFoundException - [x] Update the
UnitTestBaseClassGenerator- I've refactored theParse/TryParsetests (completing the test coverage) - having a look at the diff on theMassTestsBase.g.cs, it looks like these account for about half of all diffs in the PR 😄 - [x] Make the
QuantityInfoLookuppublic: apart from the extra constructors, there doesn't appear to be any other differences- and I don't see any reason to keep it internal - [ ] Introduce the
CodeGen/Helpers/ExpressionEvaluationHelpers.cs+CodeGen/Helpers/ExpressionAnalyzerand replace the unit conversion expressions such as(_value / 1e3) * 1e-2d)with the simplified expression (unless we actually plan to use the the rest of this PR- this would probably be an overkill). - [ ] The
JsonQuantityConverterstuff fromSystemTextcould theoretically come with it'sdoubleversions first (but we do need to have a discussion about it)
Hopefully by the time we get to 5) you'd be up to speed (and fed up with PRs) and we can turn back to reviewing / working on the rest of it as a whole 😄
Ok, sounds good. Just send PRs my way and I'll try to get to them. I have a little bit of extra time this weekend.
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.
This PR was automatically closed due to inactivity.
@angularsen I've synced the changes with the main branch, updated the header-comment and the task-list. Having a look at the changes, things don't look so scary anymore... I did notice a few bits of commented-code which I'll clean up tomorrow, and then move down the task-list.
Great! I may have some time this weekend to look at it.
@angularsen By removing the EnumUtils in https://github.com/angularsen/UnitsNet/commit/7264a1f5f2c83eff613a72b0f795a8529ea7ca19 you broke my .net48 tests. In master we aren't testing net framework in the main tests project, but given how many #if NET checks we have, I thought it would make sense to include it here (despite how terribly slow those tests are, compared to .net 8+).
So how do you wanna handle this? I could keep the EnumUtils and add an #if NET optimization in its body?
With #1543 there should be a new EnumHelper marked internal, can you reuse that?
Otherwise, for tests just add whatever you need, no big deal.
@angularsen FYI - I'm keeping the nugget version in this PR one step ahead of the official pre-release version so that the nugget references in the Samples projects pick up the right one (same goes for my nugget sources).
PS Before running the Samples, make sure you have built the Artifacts (from the global build script).
@angularsen I've synced the changes from upstream, however instead of bringing down the diffs, this has added another ~200 file changes (the new NumberExtensions).
I've updated the task list (completing the IQuantity interface refactoring). The next task on the list is
Introduce the CodeGen/Helpers/ExpressionEvaluationHelpers.cs + CodeGen/Helpers/ExpressionAnalyzer and replace the unit conversion expressions such as (_value / 1e3) * 1e-2d) with the simplified expression (unless we actually plan to use the the rest of this PR- this would probably be an overkill).
... but that can only start after I've had a confirmation, that we're moving towards replacing the double (in v6).
@lipchev I really like the concept of the decimal fractions, it solves equality issues and precision, and if I recall correctly it remains fairly backwards compatible.
I want to say let's go, but if you could please help me out and give a short summary for a couple of things that would help a lot as I don't recall and I'm pressed for time.
- How does it affect performance? CPU time on creating a quantity, converting units and ToString are perhaps the big ones. Allocations are also relevant. I don't need full benchmarks, just a top level understanding of are we talking 2x, 10x, 100x orders of magnitude.
- Any significant breaking changes to be aware of?
* How does it affect performance? CPU time on creating a quantity, converting units and ToString are perhaps the big ones. Allocations are also relevant. I don't need full benchmarks, just a top level understanding of are we talking 2x, 10x, 100x orders of magnitude.
I spent a full Sunday back in February creating a comparison chart, but before I could finish up setting up all the sheets, I accidentally executed the build script which erased all of my benchmark results. 😠 I'll attach the file as it is, but the gist is that compared to v5 there should be a significant performance improvement, especially when it comes to the strings.
UnitsNet-Benchmarks.xlsx
The conversions are about the same, or faster (with units other than the BaseUnit). There are no allocations, as long as the Value is represented by a fraction, who's terms are smaller than int.MaxValue.
For example the value 42.24 is represented as 4224 / 100, which when multiplied by 1000 (some Kilo conversion) would become 4224000 / 100 which is still small enough. Note that I intentionally avoid the (possible) reduction, since keeping the original denominator generally makes the additions / subtractions more efficient: for example adding 1.23 to would result in 4224123 / 100 (simply adding the integers in the numerators). So, while dividing 4224000 / 100 by 1000 would revert back to 4224 / 100, doing a bunch of multiplications (without manually calling QuantityValue.Reduce) would sooner or later hit the magical threshold (int.MaxValue) which would trigger the allocation of the uint[]? _bits. From there on, the performance scales linearly with the size of the bits increasing by 1 for every power of int.MaxValue (my PC was still able to do square roots, even after something like 500K digits of precision). Anyway, I've got a ton of benchmarks of the implementation over at the Fractions repo (with loads of pretty charts) - when we factor in the occasional allocations, the raw arithmetic-performance was within the same order of magnitude as the decimal (it was 2x-6x slower back on Jul 1, 2024, and I expect the QuantityValue to be slightly faster).
The initialization is slower but no more than 10x, judging from these benchmarks but that can be reduced by loading a select subset of quantities (I imagine loading just 10 QuantityInfos would still at least 2x faster).
PS The Inverse method is also something like 2x slower (but that's due to the extra logic)...
PS2 Aside for the static benchmarks, I've also observed a visible improvement (in the performance logs) after migrating my code-base, but that's probably due to the improvements to the string handling stuff around the UnitParser, UnitAbbreviations etc. (from v5 to v6).
* Any significant breaking changes to be aware of?
Compared to the v5 the only significant change is that
double value = Mass.Zero.Valueis not alloweddecimal value = Power.Zero.Valueis not allowed both of these return aQuantityValuewhich does not support the implicit conversion todoubleordecimal:double value = (double)Mass.Zero.Valueis alloweddecimal value = Power.Zero.Value.ToDecimal()is also allowed
While inconvenient, this is the correct way to do it (implicit conversions from, and explicit conversion to any primitive number).
By the way, given that in v5 IQuantity.Value is still defined as QuantityValue (without an explicit cast as far as I remember), this shouldn't be that much of surprise 🤷
Thank you. I'll post some thoughts before reviewing the actual PR code changes.
Excel sheet
Nice overview, here is my read of it:
- Perf improvements for all but ToUnit
- The biggest absolute improvements:
- Parse/TryParse is ~60% faster (49000 to 18000 ns)
- ToString is ~75% faster (700 to 150 ns)
- SI/UnitSystem overloads are 45% faster (200 to 120 ns)
- ToUnit is 60% slower, but in absolute terms negligible (+15 ns)
Normalizing
Regarding not normalizing and running into allocations when crossing int.MaxValue, this is useful to know (new to me) and something worth documenting in v6 release. We'll need to explain the pros/cons of decimal fractions anyway.
There are maybe ways to optimize when to normalize and not? Either way, manual control of normalize is nice for those who care about it.
Initialization
Can't we just lazy initialize the quantities actually used? Not sure how much work that would be.
Breaking changes
Good overview. What are the disadvantages of supporting implicit cast to double and decimal though? It seems really convenient to me.
Initial review by Claude.
PR Review: QuantityValue as Fractional Number 🐲
PR: #1544 Reviewer: Claude Date: 2025-10-11 Status: Conceptual Approval ✅ / Implementation Conditional ⚠️
Overview
This is a massive, fundamental rewrite of UnitsNet's core architecture:
- 956 files changed: +104,884 insertions / -55,631 deletions
- Build Status: ✅ Compiles successfully
- Breaking Changes: 🔴 Affects 100% of public API
Key Changes
1. QuantityValue → Fractional Representation
QuantityValue is now backed by BigInteger numerator/denominator instead of double:
// Internal representation
private readonly BigInteger _numerator;
private readonly BigInteger? _denominator;
// Example: Exact fractions instead of floating-point
new QuantityValue(5000, 127) // Exact: 5000/127 for inches
Benefits:
- Arbitrary precision without loss of accuracy
- Eliminates floating-point rounding errors
- Exact conversions (e.g.,
1/3stays exact, not0.333...) - Mathematically sound representation
2. API Breaking Changes
| Change | Before | After |
|---|---|---|
| Value type | double Value |
QuantityValue Value |
| As() return | double As(unit) |
QuantityValue As(unit) |
| Division operator | Length / Length → double |
Length / Length → QuantityValue |
| Constructor | new Length(double, unit) |
new Length(QuantityValue, unit) |
| Interface methods | IQuantity.ToUnit() |
Extension methods |
| Static property | DefaultConversionFunctions |
[Obsolete] → UnitConverter.Default |
3. New Features
- ✅ New
UnitsNet.Serialization.SystemTextJsonpackage with multiple converters - ✅ Multiple
UnitConverterimplementations (Frozen, Dynamic, NoCaching) - ✅ Configurable caching strategies via
QuantityConverterBuildOptions - ✅ Enhanced
UnitsNetSetupwith extensive configuration options - ✅ Comprehensive samples in
Samples/UnitsNetSetup.Configuration/
4. Architecture Improvements
UnitInfonow hasConversionFromBase/ConversionToBaseproperties (eliminates switch statements)- Better separation of concerns with extension methods
- Cleaner interface hierarchy with
IQuantityOfType<T> - Improved generated code structure
Migration Impact
Breaking Changes Examples
Every consumer will need code changes:
// ❌ Breaking - no longer compiles
double meters = length.Value;
double ratio = length1 / length2;
var newLength = new Length(5.0, LengthUnit.Meter);
void ProcessDistance(double meters) { }
ProcessDistance(length.Meters);
// ✅ Migration needed
QuantityValue meters = length.Value;
double metersAsDouble = length.Value.ToDouble();
QuantityValue ratio = length1 / length2;
var newLength = new Length(new QuantityValue(5), LengthUnit.Meter);
void ProcessDistance(double meters) { }
ProcessDistance(length.Meters.ToDouble());
Interface Changes
// ❌ Removed from IQuantity
double As(Enum unit);
double As(UnitKey unitKey);
IQuantity ToUnit(Enum unit);
IQuantity<T>.ToUnit(UnitSystem unitSystem);
// ✅ Now available as extension methods
QuantityExtensions.As(this IQuantity quantity, UnitKey unitKey);
QuantityExtensions.ToUnit(this IQuantity quantity, UnitKey unitKey);
QuantityExtensions.ToUnit<T>(this IQuantity<T> quantity, UnitSystem unitSystem);
Serialization Compatibility
Good News 🎉
JSON serialization is mostly backwards compatible with the right configuration:
// Old format (v5): {"Value":10.0,"Unit":"m"}
// New format (v6): {"Value":10,"Unit":"m"}
// Deserialize old data with RoundedDouble option:
var converter = new AbbreviatedUnitsConverter(
new QuantityValueFormatOptions(
SerializationFormat: QuantityValueSerializationFormat.DoublePrecision,
DeserializationFormat: QuantityValueDeserializationFormat.RoundedDouble
));
Length length = JsonConvert.DeserializeObject<Length>(oldJson, converter);
// ✅ Can read old JSON!
Default Behavior Change ⚠️
Important: The default AbbreviatedUnitsConverter() constructor behavior has changed:
// Old code (v5) - using default constructor
var converter = new AbbreviatedUnitsConverter();
var json = JsonConvert.SerializeObject(Length.FromMeters(10), converter);
// v5 output: {"Value":10.0,"Unit":"m","Type":"Length"}
// Same code in v6 - uses new defaults
var converter = new AbbreviatedUnitsConverter(); // Now defaults to DecimalPrecision/ExactNumber
var json = JsonConvert.SerializeObject(Length.FromMeters(10), converter);
// v6 output: {"Value":10,"Unit":"m","Type":"Length"} // Still compatible for simple values
// BUT for fractional values:
var json = JsonConvert.SerializeObject(Length.FromMeters(1.0/3.0), converter);
// v5 output: {"Value":0.333333333333333,"Unit":"m","Type":"Length"}
// v6 output: {"Value":0.3333333333333333333333333333,"Unit":"m","Type":"Length"} // 29 digits!
Migration for existing codebases:
// If you want v5-compatible behavior (17 digit precision):
var converter = new AbbreviatedUnitsConverter(
new QuantityValueFormatOptions(
SerializationFormat: QuantityValueSerializationFormat.DoublePrecision,
DeserializationFormat: QuantityValueDeserializationFormat.RoundedDouble
));
// If you want exact round-trip precision:
var converter = new AbbreviatedUnitsConverter(); // Uses DecimalPrecision/ExactNumber (default)
// If you want exact fractional notation (e.g., "1/3"):
var converter = new AbbreviatedUnitsConverter(
new QuantityValueFormatOptions(
SerializationFormat: QuantityValueSerializationFormat.RoundTripping,
DeserializationFormat: QuantityValueDeserializationFormat.RoundTripping
));
Serialization Format Options
The PR provides multiple serialization strategies:
- DoublePrecision - Up to 17 significant digits (compatible with old format)
- DecimalPrecision - Up to 29 significant digits
- RoundTripping - Exact fractional notation (e.g.,
"1/3"as string) - Custom - Use custom converters
Migration Needed For
- DataContract binary serialization (internal
_valuefield changed fromdoubletoQuantityValuestruct) - Custom serializers that directly depended on
Valuebeing adouble - Code using
BigIntegerserialization (field names are"N"and"D"for numerator/denominator)
Concerns & Questions
1. Performance Impact
Concern: BigInteger operations are significantly slower than double arithmetic.
Questions:
- What are the benchmark results comparing v5 vs v6 for common operations?
- What's the performance impact on arithmetic operations?
- What's the impact on conversions between units?
- What's the startup/initialization time difference?
Mitigation: The PR provides multiple caching strategies (Frozen, Dynamic, NoCaching) which is excellent.
2. Memory Usage
Concern: BigInteger pairs require more memory than double primitives.
Questions:
- What's the memory footprint comparison?
- What's the GC pressure impact?
- How does this scale with large collections of quantities?
3. Test Status
Status: ✅ All tests pass (confirmed by PR author in comments)
Note: Test execution time is significantly longer due to comprehensive test coverage and BigInteger operations, but all tests are passing.
4. Documentation Gap
Concern: While samples exist, comprehensive migration documentation is needed.
Required Documentation:
- Step-by-step migration guide from v5 to v6
- Breaking changes with before/after examples
- Performance considerations and optimization strategies
- Serialization migration approach
- API comparison table
5. Reducing Breaking Changes Impact
Current Reality: This breaks 100% of existing code using the library.
However, the PR includes implicit/explicit conversions that could significantly reduce the impact:
Already Implemented ✅
// Implicit conversions TO QuantityValue (from numbers)
public static implicit operator QuantityValue(double value);
public static implicit operator QuantityValue(decimal value);
public static implicit operator QuantityValue(int value);
public static implicit operator QuantityValue(long value);
// ... and all other numeric types
// Explicit conversions FROM QuantityValue (to numbers)
public static explicit operator double(QuantityValue value);
public static explicit operator decimal(QuantityValue value);
public double ToDouble();
public decimal ToDecimal();
Impact Analysis
With these conversions, some breaking changes are mitigated:
✅ Constructor calls work (implicit conversion):
// ✅ This still works due to implicit conversion
var length = new Length(5.0, LengthUnit.Meter); // double → QuantityValue implicitly
✅ Passing doubles to methods works:
void ProcessLength(Length length) { }
ProcessLength(Length.FromMeters(5.0)); // Works! double → QuantityValue implicitly
❌ But these still break (require explicit conversion):
// ❌ Still breaks - no implicit conversion from QuantityValue to double
double meters = length.Value; // Error
double meters = length.Meters; // Error
// ✅ Need explicit conversion
double meters = (double)length.Value; // Explicit cast
double meters = length.Value.ToDouble(); // Method call
❌ Property returns still break:
// ❌ Properties return QuantityValue, not double
public QuantityValue Meters { get; } // Not double anymore
Potential Further Mitigation
Question: Should we add implicit conversion FROM QuantityValue to double?
// Potential addition:
public static implicit operator double(QuantityValue value) => value.ToDouble();
Pros:
- Would make
double meters = length.Value;work without changes - Would make
double ratio = length1 / length2;work - Significantly reduces breaking changes (~80% of code might work as-is)
Cons:
- Loss of precision warning at compile-time (implicit conversion from exact to approximate)
- Could hide performance issues (converting to double on every access)
- Philosophically questionable (lossy conversion being implicit)
- May encourage anti-patterns (defeating the purpose of exact arithmetic)
Revised Impact Estimate
With current implicit/explicit conversions:
- ~60-70% of code needs changes (property accesses, division operators)
With implicit double conversion (if added):
- ~20-30% of code needs changes (mainly where QuantityValue type is exposed)
Recommendations
Required for Merge
- [ ] Migration guide with comprehensive before/after examples
- [ ] Performance benchmarks published (comparison with v5)
- Arithmetic operations
- Unit conversions
- Serialization/deserialization
- Memory usage
- Startup time
- [x] Confirm all tests pass and execution time is acceptable
- [ ] Breaking changes documentation complete and published
- [x] Version bump to 6.0.0
Strongly Recommended
- [ ] Consider adding implicit conversion from QuantityValue to double/decimal
- See detailed analysis in section 5 "Reducing Breaking Changes Impact"
- Would reduce breaking changes from ~60-70% to ~20-30% of code
- Trade-off: Implicit lossy conversion vs migration burden
- Alternative: Keep explicit only but provide excellent migration tooling
- [ ] Conversion helpers (already implemented, but ensure well-documented):
// These already exist in the PR: public static explicit operator double(QuantityValue value); public static explicit operator decimal(QuantityValue value); public double ToDouble(); public decimal ToDecimal(); - [ ] Roslyn analyzer to help automate migration
- Flag usage of
.Valueexpectingdouble - Flag constructors passing
doubleliterals - Suggest fixes with code actions
- Flag usage of
Optional but Valuable
- [ ] Community feedback period before final release
- [ ] Performance optimization guide (when to use Frozen vs Dynamic converters)
Detailed Code Review Notes
QuantityValue Implementation
File: UnitsNet/CustomCode/Value/QuantityValue.cs
✅ Excellent:
- Clean implementation using
BigIntegernumerator/denominator - Proper normalization handling
- Comprehensive arithmetic operations
- Power-of-ten optimization with
PowerOfTen()helper - Proper
[DataContract]attributes with names"N"and"D"
Generated Code Quality
File: UnitsNet/GeneratedCode/Quantities/Length.g.cs
✅ Improvements:
- Conversion values are now exact fractions (e.g.,
new QuantityValue(1250, 381)for feet) - Cleaner property generation
- Better debugger display with
QuantityDebugProxy - Proper
EmitDefaultValue = falseon[DataMember]attributes
UnitConverter Redesign
File: UnitsNet/UnitConverter.cs
✅ Architecture Win:
- Multiple implementations for different use cases
FrozenQuantityConverterfor optimal performanceDynamicQuantityConverterfor runtime flexibilityNoCachingConverterfor minimal memory footprint- Excellent use of
FrozenSeton .NET 8+
Serialization
Files: UnitsNet.Serialization.SystemTextJson/, UnitsNet.Serialization.JsonNet/
✅ Comprehensive:
- Multiple converters for different formats
- Backwards compatibility considerations
QuantityValueFormatOptionsfor fine-grained control- Good test coverage
Verdict
Technical Assessment
Architecture: ⭐⭐⭐⭐⭐ (5/5)
- Excellent design with fractional representation
- Multiple converter implementations
- Clean separation of concerns
- Well-thought-out configuration options
Code Quality: ⭐⭐⭐⭐⭐ (5/5)
- Clean implementation
- Comprehensive test coverage
- Good use of modern C# features
- Proper XML documentation
Breaking Changes Impact: ⭐⭐ (2/5)
- Affects 100% of consumers
- No gradual migration path
- Significant community disruption expected
Documentation: ⭐⭐⭐ (3/5)
- Good samples provided
- Missing comprehensive migration guide
- Needs performance documentation
Overall Recommendation
Conceptual Approval ✅
The fractional approach is:
- Mathematically sound
- Solves real precision problems
- Eliminates floating-point errors
- Well-architected
Implementation Approval ⚠️ CONDITIONAL
This is high-quality, transformative work, but requires:
- Clear migration path - Comprehensive documentation and tooling
- Performance validation - Published benchmarks showing real-world impact
- Extended testing - Multiple alpha/beta releases with community feedback
- Community buy-in - This is a breaking change that affects everyone
Final Thoughts
The 🐲 dragon emoji in the title is well-earned - this is indeed a beast of a change!
This represents a fundamental reimagining of UnitsNet's core value type. The precision benefits are real and valuable:
- Exact conversions - No more
0.30479999999999996when you mean1250/4096 - Arbitrary precision - No limits on value range or precision
- Mathematically correct - Fractional arithmetic is exact
However, the migration burden is enormous:
- Every line of code using UnitsNet needs updating
- Every persisted dataset may need migration
- Every API boundary needs careful handling
- Every performance assumption needs re-validation
This is the right direction, but needs careful execution.
Review Completed: 2025-10-11 Build Verified: ✅ Success Tests Verified: ✅ All pass Recommendation: Approve with conditions listed above
Would love to see this succeed! 🚀
Questions for PR Author
- What are the performance benchmark results? (especially common operations)
- Should we consider adding implicit conversion from
QuantityValuetodoubleto reduce breaking changes? (See section 5 analysis above) - Is there a migration guide in progress?
- Will there be migration tooling (Roslyn analyzer)?
I saw quite a bit of commented code here and there, so I asked Claude to identify all the commented out parts. Something to consider going over.
PR #1544 - Commented Code Review
PR Title: QuantityValue implemented as a fractional number 🐲 Author: @lipchev Status: Open
Summary
This review identifies commented-out code in PR #1544 that should either be removed or have TODO comments explaining why it's commented out and what the plan is for uncommenting later.
🔴 High Priority - Remove Immediately
1. QuantitiesSelector.cs - Duplicate Commented Class (112 lines!)
File: UnitsNet/CustomCode/QuantityInfo/Builder/QuantitiesSelector.cs
Lines: 133-244
Issue: Entire duplicate class definition (~112 lines) commented out with no explanation
// /// <summary>
// /// Provides functionality to select and configure quantities for use within the UnitsNet library.
// /// </summary>
// ... [full duplicate class implementation - 112 lines]
// }
Recommendation: DELETE - This appears to be an old version of the same class that should have been removed. Git history preserves this for reference if needed.
2. ExpressionEvaluator.cs - "No Longer Necessary" Code
File: CodeGen/Helpers/ExpressionAnalyzer/ExpressionEvaluator.cs
Lines: 278-283
// these are no longer necessary
// var expressionEvaluator = new ExpressionEvaluator(parameter, constantExpressions,
// new SqrtFunctionEvaluator(),
// new PowFunctionEvaluator(),
// new SinFunctionEvaluator(),
// new AsinFunctionEvaluator());
var expressionEvaluator = new ExpressionEvaluator(parameter, constantExpressions);
Recommendation: DELETE - Explicitly marked as "no longer necessary". Git history preserves the old constructor call pattern.
🟡 Medium Priority - Remove or Add TODO
3. QuantitiesSelector.cs - Unused Property
File: UnitsNet/CustomCode/QuantityInfo/Builder/QuantitiesSelector.cs
Line: 30
// internal Lazy<IEnumerable<QuantityInfo>> QuantitiesSelected { get; }
Recommendation: DELETE - No explanation provided; appears to be leftover from refactoring.
4. QuantitiesSelector.cs - ToList() Alternative
File: UnitsNet/CustomCode/QuantityInfo/Builder/QuantitiesSelector.cs
Line: 98
return enumeration;
// return enumeration.ToList();
Recommendation: Either:
- DELETE if
ToList()is definitely not needed, OR - ADD TODO:
// TODO: Consider materializing with ToList() if multiple enumerations cause performance issues
5. DynamicQuantityConverter.cs - Old Implementation (2 instances)
File: UnitsNet/CustomCode/QuantityConverters/DynamicQuantityConverter.cs
Line 58:
// TryGetUnitInfo(conversionKey.FromUnitKey with { UnitValue = conversionKey.ToUnitValue }, out UnitInfo? toUnitInfo))
Line 347:
// return targetQuantityInfo.Create(conversionFunction.Convert(value), conversionFunction.TargetUnit);
return targetQuantityInfo.From(conversionFunction.Convert(value), conversionFunction.TargetUnit.ToUnit<TTargetUnit>());
Recommendation: Either:
- DELETE if old implementations are not needed, OR
- ADD TODO explaining why preserved (e.g., performance comparison, alternative approach)
6. FrozenQuantityConverter.cs - Old Implementation
File: UnitsNet/CustomCode/QuantityConverters/FrozenQuantityConverter.cs
Line: 67
// TryGetUnitInfo(conversionKey.FromUnitKey with { UnitValue = conversionKey.ToUnitValue }, out UnitInfo? toUnitInfo))
Recommendation: Same as DynamicQuantityConverter - either DELETE or add TODO with explanation.
7. FractionExtensions.cs - Old Double-Based Approach
File: CodeGen/Helpers/ExpressionAnalyzer/Functions/Math/FractionExtensions.cs
Line: 35-36
// return FromDoubleRounded(System.Math.Pow(x.ToDouble(), power.ToDouble()));
return PowRational(x, power);
Recommendation: Either:
- DELETE if
PowRationalis proven stable, OR - ADD TODO:
// TODO: Old double-based approach for reference - remove once PowRational is proven stable in production
✅ Keep (Already Have Adequate Explanation)
ConversionExpression.cs - Inline Documentation
File: UnitsNet/CustomCode/QuantityInfo/Units/ConversionExpression.cs
Lines: 264, 268, 276, 282, 293, 297, 305, 311, 322, 328, 336, 342
These comments show simplified lambda forms that enhance readability. Keep as-is.
// scaleFunction = value => value;
// scaleFunction = value => value * coefficient;
UnitsNetSetup.cs - Future Work TODOs
File: UnitsNet/CustomCode/UnitsNetSetup.cs
Lines: 35-38
// TODO see about allowing eager loading
// private AbbreviationsCachingMode AbbreviationsCaching { get; set; } = AbbreviationsCachingMode.Lazy;
// TODO see about caching the regex associated with the UnitParser
// private UnitsCachingMode UnitParserCaching { get; set; } = UnitsCachingMode.Lazy;
Status: ✅ Properly documented future work. Keep as-is.
UnitTestBaseClassGenerator.cs - Documented Limitation
File: CodeGen/Generators/UnitsNetGen/UnitTestBaseClassGenerator.cs
Lines: 1138-1139
// note: it's currently not possible to test this due to the rounding error from (quantity - otherQuantity)
// Assert.True(quantity.Equals(otherQuantity, maxTolerance));
Status: ✅ Well-documented limitation. Keep as-is.
MainWindowVM.cs - Sample Code Alternative Approach
File: Samples/UnitConverter.Wpf/UnitConverter.Wpf/MainWindowVM.cs
Lines: 135-136
// note: starting from v6 it is possible to store (and invoke here) a conversion expression
// ConvertValueDelegate _convertValueToUnit = UnitsNet.UnitConverter.Default.GetConversionFunction(...);
// ToValue = _convertValueToUnit(FromValue);
Status: ✅ Sample/demo code showing users an alternative v6 feature. Keep as-is.
ExpressionEvaluationHelpers.cs - Implicit Constructor Note
File: CodeGen/Helpers/ExpressionEvaluationHelpers.cs
Line: 354
// return $"new ConversionExpression({coefficientTermFormat})";
return coefficientTermFormat; // using the implicit constructor from QuantityValue
Status: ✅ Has explanation, though could be slightly improved:
Optional improvement:
// Old explicit approach: return $"new ConversionExpression({coefficientTermFormat})";
return coefficientTermFormat; // using the implicit constructor from QuantityValue
Summary Statistics
- 🔴 High Priority Deletions: 2 instances (~118 lines)
- 🟡 Medium Priority (Remove or TODO): 7 instances
- ✅ Keep (Adequate documentation): 6 categories of comments
Recommended Actions
- Delete the 112-line duplicate class in
QuantitiesSelector.cs(lines 133-244) - Delete "no longer necessary" code in
ExpressionEvaluator.cs(lines 278-283) - Review the 7 medium-priority items and either:
- Delete if no longer needed, or
- Add clear TODO comments explaining preservation reason
This will remove ~125+ lines of dead code and improve code maintainability.
@angularsen I expected the build would fail, but regardless went on and pushed the target framework to net10.0. It all works fine (on my machine 😄 ), with the exception of the NanoFramework build which I wasn't able to test (Microsoft boasts that every extension would be compatible, but who knows 🤷 ).
I ran a couple of the benchmarks - not everything is faster, but there is a good amount of improvement around the Parse/Format stuff. Anyway, I've already updated the package references in my own solutions (multi targeting net472 and net10.0 😄 ) and ran a few tests - everything looks good.
PS I'm still experiencing issues with the resharper extension not being able to properly evaluate some expressions in the debugger (no issues with the native VS tooltips)- I've got 2 issues open on their tracker, please check that the debug-proxies are all still working fine for you in raider.