public bool Equals(HashSet <T>?x, HashSet <T>?y) { // If they're the exact same instance, they're equal. if (ReferenceEquals(x, y)) { return(true); } // They're not both null, so if either is null, they're not equal. if (x == null || y == null) { return(false); } EqualityComparer <T> defaultComparer = EqualityComparer <T> .Default; // If both sets use the same comparer, they're equal if they're the same // size and one is a "subset" of the other. if (HashSet <T> .EqualityComparersAreEqual(x, y)) { return(x.Count == y.Count && y.IsSubsetOfHashSetWithSameComparer(x)); } // Otherwise, do an O(N^2) match. foreach (T yi in y) { bool found = false; foreach (T xi in x) { if (defaultComparer.Equals(yi, xi)) { found = true; break; } } if (!found) { return(false); } } return(true); }