Skip to content

IEquatable should not be used on both parent and child #9972

Closed

Description

@matgrioni

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.

Activity

  1. added and removed
    ⌚ Not Triaged
    on Feb 12, 2019
  2. BillWagner commented on Feb 12, 2019

    @BillWagner
    Member

    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.

  3. added this to the Backlog milestone on Feb 12, 2019
  4. matgrioni commented on Feb 19, 2019

    @matgrioni
    Author

    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)
    
  5. added
    won't fix
    on Jan 25, 2021
  6. dotnet-bot commented on Jan 25, 2021

    @dotnet-bot
    Contributor

    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.

  7. removed
    won't fix
    on Mar 29, 2021
  8. self-assigned this
    on Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions