为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

C#
牛魔王的故事 2021-06-04 19:22:19
我正在将两个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());当我运行上面的代码时,断点从未被触发。有什么问题?
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 140 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信