You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
IEquatable should not be used on both parent and child #9972
IEquatable should not be used on both the parent and child class definitions. This will lead to nasty results, and bugs for clients. For example, if we define pointE as (3, 4, 6), now all of a sudden pointE equals any other compile time 2D point with (3, 4) as the first two coordinates, if pointE's compile time type is 2D point.
IEquatable should only be used on child, sealed classes so that polymorphism will work as expected for equality definitions. Please correct me if this is a misunderstanding.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
ID: e95b738e-a319-8947-c098-692068c24b9c
Version Independent ID: 227a6999-9add-4a0a-98cb-c0c6624d6089
Thank for pointing this out @matgrioni You are correct that one way to ensure correctness is to only implement IEquatable on leaf classes. Another option is to use runtime type checking, which would preserve the symmetric property on equality.
I've added this to our backlog to address when we update this topic.
Thanks for the response Bill. A note about using runtime type checking.
In this example runtime type checking is being used, but by virtue of the fact that Equals(TwoDPoint) is defined and Equals(ThreeDPoint) is defined we get issues. Since C# will choose the method implementation based on compile time type we can end up using 2D equality when the runtime type is 3D point. This may be what is intended in a class design, but it seems like it is bad practice. In fact from what I see, the current implementation doesn't exhibit transitivity.
TwoDPoint p1 = new ThreeDPoint(1, 2, 4);
TwoDPoint p2 = new ThreeDPoint(1, 2, 3);
object p3 = new ThreeDPoint(1, 2, 3);
Console.WriteLine(p1.Equals(p2)); // True, calls TwoDPoint Equatable implementation, and doesn't include 3D point logic (the issue)
Console.WriteLine(p2.Equals(p3)); // True, calls Equals(object) override on 3D point which calls ThreeDPoint IEquatable implementation and checks all 3 coordinates (good / expected)
Console.WriteLine(p1.Equals(o3)); // False, calls ThreeDPoint override of Equals(object) which then calls IEquatable implementation which checks all coordinates (good / expected)
This issue has been closed as part of the issue backlog grooming process outlined in #22351.
That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the @dotnet/docs team for visibility.
Activity
BillWagner commentedon Feb 12, 2019
Thank for pointing this out @matgrioni You are correct that one way to ensure correctness is to only implement
IEquatableon leaf classes. Another option is to use runtime type checking, which would preserve the symmetric property on equality.I've added this to our backlog to address when we update this topic.
matgrioni commentedon Feb 19, 2019
Thanks for the response Bill. A note about using runtime type checking.
In this example runtime type checking is being used, but by virtue of the fact that
Equals(TwoDPoint)is defined andEquals(ThreeDPoint)is defined we get issues. Since C# will choose the method implementation based on compile time type we can end up using 2D equality when the runtime type is 3D point. This may be what is intended in a class design, but it seems like it is bad practice. In fact from what I see, the current implementation doesn't exhibit transitivity.dotnet-bot commentedon Jan 25, 2021
This issue has been closed as part of the issue backlog grooming process outlined in #22351.
That automated process may have closed some issues that should be addressed. If you think this is one of them, reopen it with a comment explaining why. Tag the
@dotnet/docsteam for visibility.