0

I have an Account class:

class Account
{
    List<Contact> Contacts;

    // other properties, constructor, etc.
}

Using Visual Studio Community 2017, when opening the quick actions menu for this class, I can choose "Generate Equals(object)".

Here is the generated Equals:

public override bool Equals(object obj)
{
    var account = obj as Account;
    return account != null &&
           EqualityComparer<List<Contact>>.Default.Equals(Contacts, account.Contacts) &&
            // checking equality for other properties
}

When I try to pass an account with the same list of contacts (same values), the method returns false.

Digging a little bit, I found that the problem is in this line:

EqualityComparer<List<Contact>>.Default.Equals(Contacts, account.Contacts)

So if for example, I pass the following lists, the above expression will be false.

var a = new List<Contact>(new Contact("Michael"), new Contact("John")); 
var b = new List<Contact>(new Contact("Michael"), new Contact("John"));

And that is understandable since the two lists have different references. How could I change it so it will check for value equality inside the list?

Note: Contact overrides Equals to check for value equality, so checking equality for two different instances of new Contact("Michael") returns true.


Edit: If I was not clear, I want the items of the lists to have value equality regardless of their order.

Michael Haddad
  • 4,085
  • 7
  • 42
  • 82
  • 1
    I think you should also override `GetHashCode` method. Have a look at this https://stackoverflow.com/a/38434457/2946329. – Salah Akbari Sep 11 '17 at 15:09
  • Do you want to iterate each list and check that they have identical contents? If they do but they are not ordered the same, are they still equal? Answer those two questions, then try writing some code. – Mark Larter Sep 11 '17 at 15:10
  • Also, in addition to the point raised by @MarkLarter, your answer is partially in the question. You probably need to override Equals for the List as well, to check if every contact in both lists are present. At a very naive level, it would be an O(n^2) test. – crazyGamer Sep 11 '17 at 15:12
  • @MarkLarter I Edited my question so it's more clear. – Michael Haddad Sep 11 '17 at 15:13
  • @crazyGamer - well, that is exactly what I need help with. Could you elaborate in an answer? – Michael Haddad Sep 11 '17 at 15:13
  • https://stackoverflow.com/questions/3669970/compare-two-listt-objects-for-equality-ignoring-order – Michael Liu Sep 11 '17 at 15:14
  • @MichaelLiu, this is not the question. I need not only to check all the items but also to somehow insert it into the `EqualityComparer`... Thanks. – Michael Haddad Sep 11 '17 at 15:15
  • @S.Akbari - it seems like this link explains how to remove duplicates... That's not what I need. – Michael Haddad Sep 11 '17 at 15:16
  • @Sipo: No, you should remove the usage of EquailtyComparer and instead compare the lists directly. – Michael Liu Sep 11 '17 at 15:16
  • @MichaelLiu - Thanks. Could you please elaborate in an answer with a code example? I am really new to these things... – Michael Haddad Sep 11 '17 at 15:18
  • 1
    @Sipo You can use that code for finding equality instead of remove duplicates, and change it based on your requirements. – Salah Akbari Sep 11 '17 at 15:19

0 Answers0