c# - SequenceEqual 为真但 HashSet.SetEquals 为假

标签 c# linq set sequence equality

我一直在尝试实现 Set 相等性(即顺序不相关的列表比较),并在阅读了诸如 this 之类的问题之后和 this ,编写了以下简单扩展:

    public static bool SetEqual<T>(this IEnumerable<T> enumerable, IEnumerable<T> other)
    {
        if (enumerable == null && other == null)
            return true;

        if (enumerable == null || other == null)
            return false;

        var setA = new HashSet<T>(enumerable);
        return setA.SetEquals(other);
    }

但是,我遇到了一个简单的数据结构,这种方法对其不起作用,而 Enumerable.SequenceEqual 却可以。

    public class Test : IEquatable<Test>
    {
        public Guid Id { get; set; }
        public List<Test> RelatedTest { get; set; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof(Test)) return false;

            return Equals((Test)obj);
        }

        public bool Equals(Test other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;

            return other.Id.Equals(Id) &&
                   RelatedTest.SetEqual(other.RelatedTest);
        }
    }

给定这个对象,这个测试成功:

    [Test]
    public void SequenceEqualTest()
    {
        var test1 = new List<Test> {new Test()};
        var test2 = new List<Test> {new Test() };

        Assert.That(test1.SequenceEqual(test2), Is.True);
    }

但是这个测试失败了:

    [Test]
    public void SetEqualTest()
    {
        var test1 = new List<Test> {new Test()};
        var test2 = new List<Test> {new Test()};

        Assert.That(test1.SetEqual(test2), Is.True);
    }

谁有解释吗?

最佳答案

是的,您没有在 Test 类中覆盖 GetHashCode,因此 HashSet 无法根据可能的相等性有效地将项目分组到桶中。有关详细信息,请参阅此问题:Why is it important to override GetHashCode when Equals method is overridden?

关于c# - SequenceEqual 为真但 HashSet.SetEquals 为假,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12557257/

相关文章:

java - HashSet 包含重复条目

c# - 在 NHibernate 中动态更改查询类型

c# - 使用 C# (WinForms) 在 DataGridView 中显示 FQL 结果

asp.net - LINQ 查询中的默认值

c# - 使用 linq 对列表进行采样

c++ - 为什么在为数组分配新值时会崩溃?

c# - 连接来自 IQueryable 和 IList<KeyValuePair> 的数据

c# - 使用 XNA 绘制矩形

c# - For vs. Linq - 性能 vs. future

c++ - 如何正确初始化 std::set<std::string>?