Skip to content

Commit 67061f1

Browse files
committed
codegen: Assume BaseUnit for default ctor and ctors without unit param
This is to avoid a breaking change for this PR. We can later change this behavior on major version bump.
1 parent 1ea4423 commit 67061f1

File tree

1 file changed

+23
-5
lines changed

1 file changed

+23
-5
lines changed

UnitsNet/Scripts/Include-GenerateQuantitySourceCode.ps1

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,16 @@ namespace UnitsNet
9393
public partial struct $quantityName : IComparable, IComparable<$quantityName>
9494
#endif
9595
{
96+
/// <summary>
97+
/// The numeric value this quantity was constructed with.
98+
/// </summary>
9699
private readonly $baseType _value;
97100
101+
/// <summary>
102+
/// The unit this quantity was constructed with.
103+
/// </summary>
104+
private readonly $($unitEnumName)? _unit;
105+
98106
/// <summary>
99107
/// The numeric value this quantity was constructed with.
100108
/// </summary>
@@ -105,22 +113,24 @@ namespace UnitsNet
105113
#endif
106114
107115
/// <summary>
108-
/// The unit this quantity was constructed with.
116+
/// The unit this quantity was constructed with -or- <see cref="BaseUnit" /> if default ctor was used.
109117
/// </summary>
110-
public $unitEnumName Unit { get; }
118+
public $unitEnumName Unit => _unit.GetValueOrDefault(BaseUnit);
111119
112120
// Windows Runtime Component requires a default constructor
113121
#if WINDOWS_UWP
114-
public $quantityName() : this(0, BaseUnit)
122+
public $quantityName()
115123
{
124+
_value = 0;
125+
_unit = BaseUnit;
116126
}
117127
#endif
118128
119129
[Obsolete("Use the constructor that takes a unit parameter. This constructor will be removed in a future version.")]
120130
public $quantityName(double $baseUnitPluralNameLower)
121131
{
122132
_value = $convertToBaseType($baseUnitPluralNameLower);
123-
Unit = BaseUnit;
133+
_unit = BaseUnit;
124134
}
125135
126136
/// <summary>
@@ -137,10 +147,14 @@ namespace UnitsNet
137147
$quantityName($baseType numericValue, $unitEnumName unit)
138148
{
139149
_value = numericValue;
140-
Unit = unit;
150+
_unit = unit;
141151
}
142152
143153
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
154+
/// <summary>
155+
/// Creates the quantity with the given value assuming the base unit $baseUnitSingularName.
156+
/// </summary>
157+
/// <param name="$baseUnitPluralNameLower">Value assuming base unit $baseUnitSingularName.</param>
144158
#if WINDOWS_UWP
145159
private
146160
#else
@@ -151,6 +165,10 @@ namespace UnitsNet
151165
152166
// Windows Runtime Component does not allow public methods/ctors with same number of parameters: https://msdn.microsoft.com/en-us/library/br230301.aspx#Overloaded methods
153167
// Windows Runtime Component does not support decimal type
168+
/// <summary>
169+
/// Creates the quantity with the given value assuming the base unit $baseUnitSingularName.
170+
/// </summary>
171+
/// <param name="$baseUnitPluralNameLower">Value assuming base unit $baseUnitSingularName.</param>
154172
#if WINDOWS_UWP
155173
private
156174
#else

0 commit comments

Comments
 (0)