-
Notifications
You must be signed in to change notification settings - Fork 385
Adding operator overloads
Andreas Gullberg Larsen edited this page Sep 2, 2022
·
6 revisions
There is a large number of operator overloads, to facilitate strongly typed computations such as Speed speed = Length.FromMeters(100) / TimeSpan.FromSeconds(9)
.
- Put operator overload in
Length.extra.cs
if the first parameter isLength
- Add a short xmldoc summary as per the example below. You can add more descriptions if it is useful.
- Add a unit test case and place it in the equivalent file
LengthTests.cs
.
The reason is to have a consistent place to find the operator overloads and the compiler complains if the containing type/file does not match any of the parameters of the operator overload method.
UnitsNet/CustomCode/Quantities/Length.extra.cs
/// <summary>Get <see cref="Volume"/> from <see cref="Length"/> times <see cref="Area"/>.</summary>
public static Volume operator *(Length length, Area area)
{
return Volume.FromCubicMeters(area.SquareMeters * length.Meters);
}
UnitsNet.Tests/CustomCode/LengthTests.cs
[Fact]
public void LengthTimesAreaEqualsVolume()
{
Volume volume = Length.FromMeters(3) * Area.FromSquareMeters(9);
Assert.Equal(volume, Volume.FromCubicMeters(27));
}