Skip to content

Commit 83063d3

Browse files
authored
Minor performance improvements to the BaseUnits class (v5) (#1452)
- avoid storing the `IsFullyDefined` value to a field (the property is only used when constructing a `UnitSystem`) - add a reference check to the `Equals` method (improves the == Undefined) - use the `HashCode` class for the `GetHashCode` method (note that the result would be different between Framework and NET projects)
1 parent c0d78cc commit 83063d3

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

UnitsNet/BaseUnits.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,6 @@ public BaseUnits(
4646
Temperature = temperature;
4747
Amount = amount;
4848
LuminousIntensity = luminousIntensity;
49-
50-
IsFullyDefined = Length is not null &&
51-
Mass is not null &&
52-
Time is not null &&
53-
Current is not null &&
54-
Temperature is not null &&
55-
Amount is not null &&
56-
LuminousIntensity is not null;
5749
}
5850

5951
/// <inheritdoc />
@@ -72,6 +64,9 @@ public bool Equals(BaseUnits? other)
7264
if (other is null)
7365
return false;
7466

67+
if (ReferenceEquals(this, other))
68+
return true;
69+
7570
return Length == other.Length &&
7671
Mass == other.Mass &&
7772
Time == other.Time &&
@@ -108,7 +103,11 @@ public bool IsSubsetOf(BaseUnits other)
108103
/// <inheritdoc />
109104
public override int GetHashCode()
110105
{
106+
#if NET
107+
return HashCode.Combine(Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity);
108+
#else
111109
return new {Length, Mass, Time, Current, Temperature, Amount, LuminousIntensity}.GetHashCode();
110+
#endif
112111
}
113112

114113
/// <summary>
@@ -198,8 +197,19 @@ string GetDefaultAbbreviation<TUnitType>(TUnitType? unitOrNull) where TUnitType
198197
public LuminousIntensityUnit? LuminousIntensity{ get; }
199198

200199
/// <summary>
201-
/// Gets whether or not all of the base units are defined.
200+
/// Gets a value indicating whether all base units are defined.
202201
/// </summary>
203-
public bool IsFullyDefined { get; }
202+
/// <remarks>
203+
/// This property returns <c>true</c> if all seven base units
204+
/// (Length, Mass, Time, Current, Temperature, Amount, and LuminousIntensity)
205+
/// are non-null; otherwise, it returns <c>false</c>.
206+
/// </remarks>
207+
public bool IsFullyDefined => Length is not null &&
208+
Mass is not null &&
209+
Time is not null &&
210+
Current is not null &&
211+
Temperature is not null &&
212+
Amount is not null &&
213+
LuminousIntensity is not null;
204214
}
205215
}

0 commit comments

Comments
 (0)