Updated to add Inheritance, Customization, and with Operator
MODIFY
src/Chapter09.Tests/Chapter09.Tests.csproj
src/Chapter09.Tests/Chapter09.Tests.csproj
<PackageReference Update="MSTest.TestAdapter" Version="3.0.2" />
<PackageReference Update="MSTest.TestFramework" Version="3.0.2" />
</ItemGroup>
- <ItemGroup>
- <Folder Include="Original09\" />
- </ItemGroup>
</Project>

MODIFY
src/Chapter09.Tests/Listing09.04.DeclaringRecordClass.Tests.cs
src/Chapter09.Tests/Listing09.04.DeclaringRecordClass.Tests.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_04.Tests;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_01;
-using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Tests;
+using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_06B;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public partial class CoordinateTests
@@ -27,7 +28,7 @@ public partial class CoordinateTests
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
- GeoCoordinate coordinate2 = new(
+ NamedCoordinate coordinate2 = new(
coordinate1.Longitude, coordinate1.Latitude, "Test");
@@ -41,4 +42,26 @@ public partial class CoordinateTests
new Angle(180, 0, 0), new Angle(180, 0, 0));
int hashCode1 = coordinate.GetHashCode();
}
+
+ [TestMethod]
+ public void With_Instance_Equivalent()
+ {
+ Coordinate coordinate1 = new(
+ new Angle(180, 0, 0), new Angle(180, 0, 0));
+ Coordinate coordinate2 = coordinate1 with { };
+ Assert.AreEqual(coordinate1, coordinate2);
+ }
+
+ [TestMethod]
+ public void With_DifferentInstance_NotEquivalent()
+ {
+ Coordinate coordinate1 =
+ new(new Angle(180, 0, 0), new Angle(180, 0, 0));
+ Angle angle = new Angle(0, 0, 0);
+ Coordinate coordinate2 = coordinate1 with
+ {
+ Latitude = angle
+ };
+ Assert.AreNotEqual(coordinate1, coordinate2);
+ }
}

MODIFY
src/Chapter09.Tests/Listing09.05.EquivalentRecordClassCode.Tests.cs
src/Chapter09.Tests/Listing09.05.EquivalentRecordClassCode.Tests.cs
namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_05.Tests;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_01;
using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_04;
-using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Tests;
+using AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_06B;
[TestClass]
public class CoordinateTests
@@ -29,7 +29,7 @@ public class CoordinateTests
{
Coordinate coordinate1 = new(
new Angle(180, 0, 0), new Angle(180, 0, 0));
- GeoCoordinate geoCoordinate = new(
+ NamedCoordinate geoCoordinate = new(
new Angle(180, 0, 0), new Angle(180, 0, 0), "GeoCoordinate");
Assert.AreEqual(coordinate1.ExternalEqualityContract, coordinate1.GetType());

RENAME
src/Chapter09.Tests/Listing09.06.CustomizingARecord.Tests.cs
src/Chapter09.Tests/Listing09.06C.CustomizingARecord.Tests.cs
-namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_06.Tests;
+namespace AddisonWesley.Michaelis.EssentialCSharp.Chapter09.Listing09_06C.Tests;
[TestClass]
public class AngleTests

MODIFY
src/Chapter09/Listing09.02.ProgrammingWithARecordStruct.cs
src/Chapter09/Listing09.02.ProgrammingWithARecordStruct.cs
@@ -30,9 +30,18 @@ public class Program
}
Angle copy = new(degrees, minutes, seconds);
-
// Records provide a custom equality operator.
Trace.Assert(angle == copy);
+
+ // The with operator is the equivalent of
+ // Angle copy = new(degrees, minutes, seconds);
+ copy = angle with { };
+ Trace.Assert(angle == copy);
+
+ // The with operator has object initializer type
+ // syntax for instantiating a modified copy.
+ Angle modifiedCopy = angle with { Degrees = 180 };
+ Trace.Assert(angle != modifiedCopy);
}
}
#endregion INCLUDE

MODIFY
src/Chapter09/Listing09.05.EquivalentRecordClassCode.cs
src/Chapter09/Listing09.05.EquivalentRecordClassCode.cs
@@ -80,6 +80,11 @@ public class Coordinate : IEquatable<Coordinate>
public Type ExternalEqualityContract => EqualityContract();
+
+ // Actual name in IL is "<Clone>$". However,
+ // you can't add a Clone method to a record.
+ public Coordinate Clone() => new(this);
+
protected Coordinate(Coordinate original)
{
Longitude = original.Longitude;

MODIFY
src/Chapter09/Listing09.09.SubtleBoxingIdiosyncrasies.cs
src/Chapter09/Listing09.09.SubtleBoxingIdiosyncrasies.cs
@@ -58,11 +58,14 @@ public class Program
object objectAngle = angle; // Box
Console.Write(((Angle)objectAngle).Degrees);
- // Example 2: Unbox, modify unboxed value, and discard value
- ((Angle)objectAngle).MoveTo(26, 58, 23);
+ // Example 2: Unbox, modify unboxed value,
+ // and discard value
+ ((Angle)objectAngle).MoveTo
+ (26, 58, 23);
Console.Write(", " + ((Angle)objectAngle).Degrees);
- // Example 3: Box, modify boxed value, and discard reference to box
+ // Example 3: Box, modify boxed value,
+ // and discard reference to box
((IAngle)angle).MoveTo(26, 58, 23);
Console.Write(", " + ((Angle)angle).Degrees);

Updated to add Inheritance, Customization, and with Operator