https://www.bbsmax.com/A/nAJvbKywJr/

引用类型比较的是引用,需要自己实现IEqualityComparer 比较器。

IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Three"};

IList<string> strList2 = new List<string>(){"One", "Two", "Three", "Four", "Three"};

bool isEqual = strList1.SequenceEqual(strList2); // returns true

IList<string> strList1 = new List<string>(){"One", "Two", "Three", "Four", "Three"};

IList<string> strList2 = new List<string>(){ "Two", "One", "Three", "Four", "Three"};

bool isEqual = strList1.SequenceEqual(strList2); // returns false

//如果是引用类型,则比较的是引用
Student std = , StudentName = "Bill" }; IList<Student> studentList1 = new List<Student>(){ std }; IList<Student> studentList2 = new List<Student>(){ std }; bool isEqual = studentList1.SequenceEqual(studentList2); // returns true Student std1 = , StudentName = "Bill" };
Student std2 = , StudentName = "Bill" }; IList<Student> studentList3 = new List<Student>(){ std1}; IList<Student> studentList4 = new List<Student>(){ std2 }; isEqual = studentList3.SequenceEqual(studentList4);// returns false 在上面的示例中,studentList1和studentList2包含相同的学生对象std。 所以studentList1.SequenceEqual(studentList2)返回true。 但是,stdList1和stdList2包含两个独立的学生对象std1和std2。 所以现在,stdList1.SequenceEqual(stdList2)将返回false,即使std1和std2包含相同的值。 要比较复杂类型(引用类型或对象)的两个集合的值,您需要实现IEqualityComperar <T>接口,如下所示。 class StudentComparer : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
if (x.StudentID == y.StudentID && x.StudentName.ToLower() == y.StudentName.ToLower())
return true; return false;
} public int GetHashCode(Student obj)
{
return obj.GetHashCode();
}
} // following returns true
bool isEqual = studentList1.SequenceEqual(studentList2, new StudentComparer());

最新文章

  1. SQL Server 数据库镜像
  2. 什么叫哈希表(Hash Table)
  3. Python 实现粒子滤波
  4. JavaScript设计模式与开发实践 - 策略模式
  5. Spring学习(一)——Spring中的依赖注入简介【转】
  6. var obj = {};var obj2 = [];var obj3;
  7. GLSL 中的光照计算
  8. guice的基本使用(一)
  9. 记一个社交APP的开发过程——基础架构选型(转自一位大哥)
  10. LED驅動芯片 兩種恒流控制方式
  11. SQLSERVER 跨服 跨库
  12. ASP.NET脚本过滤-防止跨站脚本攻击(收集别人的)
  13. Css技术入门笔记02
  14. TP5.x——update更新成功但是返回是0
  15. react-踩坑记录——Link带参数跳转后this.props为空对象
  16. Zynq ZC706 传统方式移植Linux -- 编译u-boot
  17. web分页打印
  18. python 获取命令行参数
  19. 〖Linux〗简单的将Shell和一些文件打包成一个单独的“可执行文件”
  20. android 关于view的onTouch和onClick同时触发解决方案

热门文章

  1. docker入门-基本概念(一)
  2. 转 Oracle 同一个字段的两值进行加减计算
  3. Django框架 + Djiango安装 + First Djiango + 常用命令
  4. Centos7.3之K8S安装初体验
  5. vue播放mu38视频兼容谷歌ie等浏览器
  6. 浅谈PageRank
  7. Redis 分布式锁,C#通过Redis实现分布式锁(转)
  8. Oracle Spatial分区应用研究之六:全局空间索引下按县分区与按省分区效率差异原因分析
  9. 修复Nginx报错:upstream sent too big header while reading response header from upstream
  10. python + selenium + unittest实现简单的UI自动化