我知道HashSet<T>.SetEquals方法,但何时以及如何使用CreateSetComparer方法?
documentation声明:“只在一个级别检查相等性;但是,您可以将其他级别的比较器链接在一起以执行更深入的相等性测试”
有什么简单的例子?
特别是,如果我正在比较的集合中的每个项目也包含一个散列集,那么CreateSetComparer的正确用法是什么?
这是我的出发点。我想知道CreateSetComparer方法是否适用以及如何正确使用它:

public class Foo : IEquatable<Foo>
{
    public string Label { get; set; }
    public string Value { get; set; }
    public override string ToString() {return String.Format("{0}:{1}", Label, Value); }

    // assume for this example that Label and Value are immutable once set;
    public override int GetHashCode(){ return ToString().GetHashCode(); }
    // simplified equality check; assume it meets my needs for this example;
    public bool Equals(Foo other){ return String.Equals(this.ToString(), other.ToString()); }
}

public class FooGroup : IEquatable<FooGroup>
{
    public int GroupIndex {get; set;}
    public HashSet<Foo> FooCollection {get; set;}

    // -----------------------------
    // Does HashSet.CreateSetComparer somehow eliminate or simplify the following code?
    // -----------------------------
    public override int GetHashCode()
    {
        int hash = GroupIndex;
        foreach(Foo f in FooCollection)
          hash = hash ^ (f.GetHashCode() & 0x7FFFFFFF);
        return hash;
    }

    public bool Equals(FooGroup other)
    {
        // ignore missing null checks for this example
        return this.GroupIndex == other.GroupIndex && this.FooCollection.SetEquals(other.FooCollection);
    }
}

public class GroupCollection : IEquatable<GroupCollection>
{
    public string CollectionLabel {get; set;}
    public HashSet<FooGroup> AllGroups {get; set;}

    // -----------------------------
    // Does HashSet.CreateSetComparer somehow eliminate or simplify the following code?
    // -----------------------------
    public override int GetHashCode()
    {
        int hash = CollectionLabel.GetHashCode();
        foreach(FooGroup g in AllGroups)
          hash = hash ^ (g.GetHashCode() & 0x7FFFFFFF);
        return hash;
    }

    public bool Equals(GroupCollection other)
    {
        // ignore missing null checks for this example
        return String.Equals(this.CollectionLabel, other.CollectionLabel) && this.AllGroups.SetEquals(other.AllGroups);
    }
}

忽略系统设计等方面的争论,一个简化的用例是:假设我提取了一组复杂的数据,如下所示:
var newSetA = new GroupCollection{ ... }
var oldSetA = new GroupCollection{ ... }

我只想检查一下:
if (newSetA.Equals(oldSetA))
  Process(newSetA);

最佳答案

让我们从“createsetcomparer什么时候有用”这个问题开始吧?你已经有了一个很好的主意:
特别是,如果我正在比较的集合中的每个项也包含一个哈希集,那么createsetcomparer的正确用法是什么?
例如,下一个示例演示了当HashSet使用其默认比较器(仅通过引用进行比较)时的默认行为:

var set1 = new HashSet<HashSet<int>>{
    new HashSet<int>{2,3,4},
    new HashSet<int>{7,8,9}
};
var set2 = new HashSet<HashSet<int>>{
    new HashSet<int>{2,3,4},
    new HashSet<int>{7,8,9},
};

set1.SetEquals(set2).Dump(); // false :-(
set1.SequenceEqual(set2).Dump(); // false
set1.SequenceEqual(set2, HashSet<int>.CreateSetComparer()).Dump(); // true

也可以将CreateSetComparerSetEquals一起使用,例如:
// the order of elements in the set has been change.
var set1 = new HashSet<HashSet<int>>(HashSet<int>.CreateSetComparer()){
    new HashSet<int>{2,3,4},
    new HashSet<int>{7,8,9}
};
var set2 = new HashSet<HashSet<int>>{
    new HashSet<int>{7,8,9},
    new HashSet<int>{2,3,4},
};

set1.SetEquals(set2).Dump(); // true :-)
set1.SequenceEqual(set2).Dump(); // false
set1.SequenceEqual(set2, HashSet<int>.CreateSetComparer()).Dump(); // false

这是通常的用法,但是CreateSetComparer提供了GetHashCode您可以利用它,尽管这不一定是更短/更干净的,您已经做了什么。
// -----------------------------
// Does HashSet.CreateSetComparer somehow eliminate or simplify the following code?
// -----------------------------
private IEqualityComparer<HashSet<FooGroup>> _ecomparer =
        HashSet<FooGroup>.CreateSetComparer();
public override int GetHashCode()
{
    int hash = CollectionLabel.GetHashCode();
    hash ^= _ecomparer.GetHashCode(AllGroups);
    return hash;
}

10-06 13:27