2

I try to create a mathematical set using HashSet. I have the following code:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        HashSet<int> A = new HashSet<int>() { 1, 2 };
        HashSet<int> B = new HashSet<int>() { 1, 2 };

        HashSet<HashSet<int>> SET = new HashSet<HashSet<int>>() { A, B };

        // Desired: 1 (A and B are expected being equal)
        // Actual:  2
        Console.WriteLine(SET.Count);
        
        Console.ReadKey();
    }
}

It seems HashSet equality is not suitable because A and B must be considered the same, but for HashSet they are different objects. How can I redefine equality for HashSet?

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
  • Does this answer your question? [How do you determine if two HashSets are equal (by value, not by reference)?](https://stackoverflow.com/questions/494224/how-do-you-determine-if-two-hashsets-are-equal-by-value-not-by-reference). It suggests: `my_hashset.SetEquals(other);`. [Documentation for SetEquals](https://msdn.microsoft.com/en-us/library/dd412096(v=vs.110).aspx) – Stef Mar 26 '22 at 11:54
  • Well I'm asking how to change the internal equality automatically used by C#. – Minimus Heximus Mar 26 '22 at 12:18

1 Answers1

4

You should explain .Net how to compare your custom data (HashSet<T> in your case) with a help of IEqualityComparer<T>:

public sealed class HashSetComparer<T> : IEqualityComparer<HashSet<T>> {
  public bool Equals(HashSet<T>? left, HashSet<T>? right) {
    if (ReferenceEquals(left, right))
      return true;
    if (left == null || right == null)
      return false;

    return left.SetEquals(right);
  }

  public int GetHashCode(HashSet<T> item) {
    //TODO: improve; simplest, but not that good implementation
    return item == null ? -1 : item.Count;
  }
}

then mention the comparing rules when creating SET:

...
HashSet<HashSet<int>> SET = new HashSet<HashSet<int>>(new HashSetComparer<int>()) { 
  A, B 
};
...
Minimus Heximus
  • 2,683
  • 3
  • 25
  • 50
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 1
    @Henk Holterman: in case of `left == null` and `right == null` we have `ReferenceEquals(left, right)` above which returns `true` as expected. `Console.Write((new HashSet>(new HashSetComparer()) { null, null}).Count);` returns `1` – Dmitry Bychenko Mar 26 '22 at 07:48