@@ -17,8 +17,8 @@ namespace UnitsNet;
1717public readonly record struct UnitKey
1818{
1919 // apparently, on netstandard, the use of auto-properties is significantly slower
20- private readonly Type _unitType ;
21- private readonly int _unitValue ;
20+ private readonly Type _unitEnumType ;
21+ private readonly int _unitEnumValue ;
2222
2323 /// <summary>
2424 /// Represents a unique key for a unit type and its corresponding value.
@@ -27,10 +27,10 @@ public readonly record struct UnitKey
2727 /// This key is particularly useful when using an enum-based unit in a hash-based collection,
2828 /// as it avoids the boxing that would normally occur when casting the enum to <see cref="Enum" />.
2929 /// </remarks>
30- public UnitKey ( Type UnitType , int UnitValue )
30+ internal UnitKey ( Type UnitEnumType , int UnitEnumValue )
3131 {
32- _unitType = UnitType ;
33- _unitValue = UnitValue ;
32+ _unitEnumType = UnitEnumType ;
33+ _unitEnumValue = UnitEnumValue ;
3434 }
3535
3636 /// <summary>
@@ -40,9 +40,9 @@ public UnitKey(Type UnitType, int UnitValue)
4040 /// This property holds the <see cref="Type" /> of the unit enumeration associated with this key.
4141 /// It is particularly useful for identifying the unit type in scenarios where multiple unit types are used.
4242 /// </remarks>
43- public Type UnitType
43+ public Type UnitEnumType
4444 {
45- get => _unitType ;
45+ get => _unitEnumType ;
4646 }
4747
4848 /// <summary>
@@ -52,9 +52,9 @@ public Type UnitType
5252 /// This property represents the unique value of the unit within its type, typically corresponding to the underlying
5353 /// integer value of an enumeration.
5454 /// </remarks>
55- public int UnitValue
55+ public int UnitEnumValue
5656 {
57- get => _unitValue ;
57+ get => _unitEnumValue ;
5858 }
5959
6060 /// <summary>
@@ -81,6 +81,36 @@ public static UnitKey Create<TUnit>(int unitValue)
8181 return new UnitKey ( typeof ( TUnit ) , unitValue ) ;
8282 }
8383
84+ /// <summary>
85+ /// Creates a new instance of the <see cref="UnitKey" /> struct for the specified unit type and value.
86+ /// </summary>
87+ /// <param name="unitType">The type of the unit, which must be an enumeration.</param>
88+ /// <param name="unitValue">The integer value representing the unit.</param>
89+ /// <returns>A new instance of the <see cref="UnitKey" /> struct.</returns>
90+ /// <exception cref="ArgumentNullException">
91+ /// Thrown if <paramref name="unitType" /> is <c>null</c>.
92+ /// </exception>
93+ /// <exception cref="ArgumentException">
94+ /// Thrown if <paramref name="unitType" /> is not an enumeration type.
95+ /// </exception>
96+ /// <remarks>
97+ /// This method is useful for creating a <see cref="UnitKey" /> when the unit type and value are known.
98+ /// </remarks>
99+ public static UnitKey Create ( Type unitType , int unitValue )
100+ {
101+ if ( unitType is null )
102+ {
103+ throw new ArgumentNullException ( nameof ( unitType ) ) ;
104+ }
105+
106+ if ( ! unitType . IsEnum )
107+ {
108+ throw new ArgumentException ( $ "Unit type must be an enumeration, but was { unitType . FullName } .") ;
109+ }
110+
111+ return new UnitKey ( unitType , unitValue ) ;
112+ }
113+
84114 /// <summary>
85115 /// Implicitly converts an enumeration value to a <see cref="UnitKey" />.
86116 /// </summary>
@@ -111,7 +141,7 @@ public static implicit operator UnitKey(Enum unit)
111141 /// </remarks>
112142 public static explicit operator Enum ( UnitKey unitKey )
113143 {
114- return ( Enum ) Enum . ToObject ( unitKey . _unitType , unitKey . _unitValue ) ;
144+ return ( Enum ) Enum . ToObject ( unitKey . _unitEnumType , unitKey . _unitEnumValue ) ;
115145 }
116146
117147 /// <summary>
@@ -129,25 +159,25 @@ public static explicit operator Enum(UnitKey unitKey)
129159 /// </remarks>
130160 public TUnit ToUnit < TUnit > ( ) where TUnit : struct , Enum
131161 {
132- if ( typeof ( TUnit ) != _unitType )
162+ if ( typeof ( TUnit ) != _unitEnumType )
133163 {
134- throw new InvalidOperationException ( $ "Cannot convert UnitKey of type { _unitType } to { typeof ( TUnit ) } .") ;
164+ throw new InvalidOperationException ( $ "Cannot convert UnitKey of type { _unitEnumType } to { typeof ( TUnit ) } .") ;
135165 }
136166
137- var unitValue = _unitValue ;
167+ var unitValue = _unitEnumValue ;
138168 return Unsafe . As < int , TUnit > ( ref unitValue ) ;
139169 }
140170
141171 private string GetDebuggerDisplay ( )
142172 {
143173 try
144174 {
145- var unitName = Enum . GetName ( _unitType , _unitValue ) ;
146- return string . IsNullOrEmpty ( unitName ) ? $ "{ nameof ( UnitType ) } : { _unitType } , { nameof ( UnitValue ) } = { _unitValue } " : $ "{ _unitType . Name } .{ unitName } ";
175+ var unitName = Enum . GetName ( _unitEnumType , _unitEnumValue ) ;
176+ return string . IsNullOrEmpty ( unitName ) ? $ "{ nameof ( UnitEnumType ) } : { _unitEnumType } , { nameof ( UnitEnumValue ) } = { _unitEnumValue } " : $ "{ _unitEnumType . Name } .{ unitName } ";
147177 }
148178 catch
149179 {
150- return $ "{ nameof ( UnitType ) } : { _unitType } , { nameof ( UnitValue ) } = { _unitValue } ";
180+ return $ "{ nameof ( UnitEnumType ) } : { _unitEnumType } , { nameof ( UnitEnumValue ) } = { _unitEnumValue } ";
151181 }
152182 }
153183
@@ -162,8 +192,8 @@ private string GetDebuggerDisplay()
162192 /// </remarks>
163193 public void Deconstruct ( out Type unitType , out int unitValue )
164194 {
165- unitType = _unitType ;
166- unitValue = _unitValue ;
195+ unitType = _unitEnumType ;
196+ unitValue = _unitEnumValue ;
167197 }
168198
169199 #region Equality members
@@ -172,21 +202,21 @@ public void Deconstruct(out Type unitType, out int unitValue)
172202 public bool Equals ( UnitKey other )
173203 {
174204 // implementing the Equality members on net48 is 5x faster than the default
175- return _unitType == other . _unitType && _unitValue == other . _unitValue ;
205+ return _unitEnumType == other . _unitEnumType && _unitEnumValue == other . _unitEnumValue ;
176206 }
177207
178208 /// <inheritdoc />
179209 public override int GetHashCode ( )
180210 {
181211 // implementing the Equality members on net48 is 5x faster than the default
182- if ( _unitType == null )
212+ if ( _unitEnumType == null )
183213 {
184- return _unitValue ;
214+ return _unitEnumValue ;
185215 }
186216
187217 unchecked
188218 {
189- return ( _unitType . GetHashCode ( ) * 397 ) ^ _unitValue ;
219+ return ( _unitEnumType . GetHashCode ( ) * 397 ) ^ _unitEnumValue ;
190220 }
191221 }
192222
0 commit comments