I was experimenting with the C# 9.0 features today and noticed that when a record has a List<Point2d>, then two records with the same items in the list are not equal anymore. Point2d is also a record here.
public sealed record Point2d
{
public Point2d(double x, double y)
{
X = x;
Y = y;
}
public double X { get; }
public double Y { get; }
}
public sealed record Polyline2d
{
private readonly List<Point2d> _corners;
public Polyline2d(IEnumerable<Point2d> corners)
{
_corners = new List<Point2d>(corners);
}
}
Polyline2d a = new (new Point2d[] { new(0, 0), new(1, 1), new(2, 2)});
Polyline2d b = new (new Point2d[] { new(0, 0), new(1, 1), new(2, 2) });
a == b => false
If I manually add the equality methods for the record and redefine the Equals() method then everything is fine again:
public bool Equals(Polyline2d other)
{
return other != null && _corners.SequenceEqual(other._corners);
}
public override int GetHashCode()
{
return 42;
}
a == b => true
Is this the correct approach for addressing this problem, or are there other considerations of record data types that I need to factor in?