Question

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?

Was it helpful?

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.

OTHER TIPS

It all depends on your own definition for when two Person instances are equal! There is no magic going on here, and implementing IEquatable on Person does not cascade in any way.

So, how do you define two Person instances as being equal within the context of your application? Is equality of surname and forename good enough? If so, that is how you should implement IEquatable. Other alternatives that are sometimes used are to define equality based on some generated primary key, or some know unique value such as National Insurance number.

yes I would implement this on every class you are using.

but as the others commented it depends a bit on what your are trying to achieve.

If you implement Equals and GetHashCode (you need both) you should normaly try to only include read-only fields into the comparison. Aside from this you have to make sure that GetHashCode plays nicely with Equals (for example not returning different values if Equals returns true) and of course Equals should be symmetric.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top