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?