0

I have two lists of HashSet and want to exclude one from the other. Should be able to achieve that using the Excepts extensions method.

say List1 is [{1,2}, {2,3}] and List2 is [{2,3}]

list1.Except(list2);

should return [{1,2}] in my scenario. However, since it compares equality by comparing the references it will return the original list1 [{1,2}, {2,3}].

I think my best bet would be to implement a IEqualityComparer but I don't know exactly what do in the GetHashCode method. On the Equals I can use the SetEquals which checks if the HashSets have the same items.

Am I tackling this wrong? Is there any other way of achieving this? The hacky way I found is having the GetHashCode returning 1.

  public int GetHashCode(HashSet<int> obj)
    {
        return 1;
    }

    public bool Equals(HashSet<int> x, HashSet<int> y)
    {
        return x.SetEquals(y);
    }

0 Answers0