Search code examples
c#classiequatable

Does IEquatable cascade?


I have a very simply question regarding IEquatable. Given the following basic classes:

public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public SalaryInformation AnnualSalaryInformation { get; set; }

    }

    public class SalaryInformation
    {
        public string TaxCode { get; set; }
        public string SalaryBand { get; set; }
        public BankInformation AccountInfo { get; set; }
    }

    public class BankInformation
    {
        public string SortCode { get; set; }
        public string AccountNumber { get; set; }
    }

If I wanted to compare to Person objects for equality how would I go about it? If I implement the IEquatable interface on the the Person class, will it automatically cascade down to any objects within that class or do I have to explicitly implement the interface on all the classes?

If I do have to implement the interface on all the classes, are there any particular 'gotchas' I should look out for, when comparing an instance of Person to another instance of Person?


Solution

  • Equality between objects can take many forms, depending on what exactly you need. To answer your question:

    No, just because Person implements IEquatable doesn't mean that SalaryInformation implements IEquatable. The relationship between Person and SalaryInformation is a composition and compositions don't care about interface implementation.

    The Equals implementation of Person thus depends on when you consider two persons to be the same:

    • Are they the same if they share the same first and last name?
    • Are they the same if they have the same SalaryInformation, too?

    Also, if you want to be able to compare objects for equality there are a few guidelines to remember:

    1. Equality is an equivalence relation, i.e. it is
      a. reflexive,
      b. symmetric and
      c. transitive.
    2. Remember that null isn't equal to any object.
    3. Always override GetHashCode too.

    If you got the hang of it, it's not hard to do right, but if you never done it all sorts of issues can arise.