Equals 和 GetHashCode 都不会在 IEqualityComparer 上被调用

我正在将两个List<Dictionary<string, object>>与我自己的IEqualityComparer<Dictionary<string, object>>实现进行比较,但是 GetHashCode 和 Equals 方法都没有被调用。


这是我自己的 IEqualityComparer 实现。


public class TestEqualityComparer : IEqualityComparer<Dictionary<string, object>>

{

    public bool Equals(Dictionary<string, object> a, Dictionary<string, object> b)

    {

        return true; // breakpoint here

    }


    public int GetHashCode(Dictionary<string, object> obj)

    {

        return 0; // breakpoint here

    }

}

这是一个实际的比较代码。


var a = new List<Dictionary<string, object>>();

var b = new List<Dictionary<string, object>>();


a.Add(new Dictionary<string, object> { ["id"] = 1, ["msg"] = "aaaaa" });

a.Add(new Dictionary<string, object> { ["id"] = 2, ["msg"] = "bbbbb" });

a.Add(new Dictionary<string, object> { ["id"] = 3, ["msg"] = "ccccc" });


b.Add(new Dictionary<string, object> { ["id"] = 1, ["msg"] = "zzzzz" });

b.Add(new Dictionary<string, object> { ["id"] = 2, ["msg"] = "bbbbb" });

b.Add(new Dictionary<string, object> { ["id"] = 4, ["msg"] = "ddddd" });


var except = a.Except(b, new TestEqualityComparer());

当我运行上面的代码时,断点从未被触发。有什么问题?


牛魔王的故事
浏览 141回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP