当前位置: 移动技术网 > IT编程>开发语言>c# > C# Distinct和重写IEqualityComparer时要知道的二三事

C# Distinct和重写IEqualityComparer时要知道的二三事

2019年07月18日  | 移动技术网IT编程  | 我要评论

我们在想对一个可枚举的对象集合进行去重操作时,一般第一个想到的就是就是linq的distinct方法。

先定义一个类,然后使用distinct方法去重

class man
    {
      public int age { get; set; }
      public string name { get; set; }
      public string adress { get; set; }
      public decimal weight { get; set; }
      public decimal height { get; set; }
    }
list<man> list = new list<man>()
      { 
      new man(){age=21,name="adam",adress="shenzhen",weight=60,height=170},
      new man(){age=21,name="adam",adress="shenzhen",weight=60,height=170}
      };
      var distinct = list.distinct();

然而去重得到的distinct集合的count依然为二,集合里依然存在两个adam。

实际上,distinct方法内进行比较的是声明的引用,而不是对象属性,就和对两个属性一模一样的对象使用equals()方法得到的是false一样。

因此我们对对象集合使用distinct方法时要使用重载distinct<tsource>(this ienumerable<tsource> source, iequalitycomparer<tsource> comparer);

要使用这个方法,我们得重写iequalitycomparer接口,再使用distinct方法:

public class mancomparer : iequalitycomparer<man>
    {
      public bool equals(man x, man y)
      {
        return x.age == y.age
          && x.name == y.name
          && x.adress == y.adress
          && x.weight == y.weight
          && x.height == y.height;
      }

      public int gethashcode(man obj)
      {
        return obj.gethashcode();
      }
    }

 var distinct = list.distinct(new mancomparer());

然而,再一次,distinct集合内依然有两个对象。

实际上,由于直接获取对象的hashcode,用hashcode进行比较的速度比 equals 方法更快,

因此iequalitycomparer内部会在使用 equals 前先使用 gethashcode 方法,在两个对象的hashcode都相同时即刻判断对象相等。

而当两个对象hashcode不相同时, equals 方法就会被调用,对要比较的对象进行判断。

由于在上例中list中的两个引用实际上是两个不同的对象,因此hashcode必定不相同

所以要触发equlas方法,我们需要改 gethashcode ,让它返回相同的常量

public class mancomparernew : iequalitycomparer<man>
    {
      public bool equals(man x, man y)
      {
        return x.age == y.age
          && x.name == y.name
          && x.adress == y.adress
          && x.weight == y.weight
          && x.height == y.height;
      }

      public int gethashcode(man obj)
      {
        return 1;
      }
    }

var distinct = list.distinct(new mancomparernew());

现在distinct集合中就只有一个man对象了,成功实现了去重。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网