需要在Linq 中对比两个对象是否相等

  1. /// <summary>
  2. /// 定义一个点
  3. /// </summary>
  4. class Point
  5. {
  6. public int x { get; set; }
  7. public int y { get; set; }
  8. public Point(int x, int y)
  9. {
  10. this.x = x;
  11. this.y = y;
  12. }
  13. }
  1. List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
  2. var result1 = list1.Where(M => M == new Point(, ));

三种对比方法均不能

  1. Point p1 = new Point(, );
  2. Point p2 = new Point(, );
  3. Console.WriteLine(p1 == p2);//False
  4. Console.WriteLine(p1.Equals(p2));//False
  5. // ReferenceEquals 方法用于对象的引用是否相等
  6. // ReferenceEquals 不能重写 注意
  7. Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
  8. p1 = p2;
  9. Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True

由于没有重写 == 运算符 和 Equals 方法,不能够 直接使用否则对比的将是对象的引用地址

需要对类进行重写,详细如下

  1.    /// <summary>
  2. /// 定义一个点,并重写对象与对象是否相等的方法
  3. /// 可用于判断对象是否相等
  4. /// eg:
  5. /// obj1 == obj2
  6. /// obj1.Equals(obj2)
  7. /// </summary>
  8. class TestPoint : IEquatable<TestPoint>
  9. {
  10. public int x { get; set; }
  11. public int y { get; set; }
  12. public TestPoint(int x, int y)
  13. {
  14. this.x = x;
  15. this.y = y;
  16. }
  17.  
  18. /// <summary>
  19. /// 重载 == 运算符
  20. /// </summary>
  21. /// <param name="p1"></param>
  22. /// <param name="p2"></param>
  23. /// <returns></returns>
  24. public static bool operator ==(TestPoint p1, TestPoint p2)
  25. {
  26. return (p1.x == p2.x) && (p1.y == p2.y);
  27. }
  28.  
  29. /// <summary>
  30. /// 重载 != 运算符
  31. /// </summary>
  32. /// <param name="p1"></param>
  33. /// <param name="p2"></param>
  34. /// <returns></returns>
  35. public static bool operator !=(TestPoint p1, TestPoint p2)
  36. {
  37. return (p1.x != p2.x) || (p1.y != p2.y);
  38. }
  39.  
  40. /// <summary>
  41. /// 重写Equals(object obj)
  42. /// </summary>
  43. /// <param name="obj"></param>
  44. /// <returns></returns>
  45. public override bool Equals(object obj)
  46. {
  47. return this.Equals(obj as TestPoint);
  48. }
  49.  
  50. /// <summary>
  51. /// 重写 计算对象的哈希值方法(自定义 这里只是示范)
         /// 该方法用于判断对象的哈希值是否相等 如对象哈希值相同 就认为两个对象 相等
  52. /// </summary>
  53. /// <returns></returns>
  54. public override int GetHashCode()
  55. {
  56. return this.x.GetHashCode() + this.y.GetHashCode();
  57. }
  58.  
  59. /// <summary>
  60. /// 继承定义Equals<T>方法
  61. /// 需要继承接口IEquatable<T>
  62. /// </summary>
  63. /// <param name="other"></param>
  64. /// <returns></returns>
  65. public bool Equals(TestPoint other)
  66. {
  67. return (this.x == other.x) && (this.y == other.y);
  68. }
  69.  
  70. }

使用大概示范

  1.        Point p1 = new Point(, );
  2. Point p2 = new Point(, );
  3. Console.WriteLine(p1 == p2);//False
  4. Console.WriteLine(p1.Equals(p2));//False
  5. // ReferenceEquals 方法用于对象的引用是否相等
  6. // ReferenceEquals 不能重写 注意
  7. Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
  8. p1 = p2;
  9. Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True
  10.  
  11. TestPoint p3 = new TestPoint(, );
  12. TestPoint p4 = new TestPoint(, );
  13. Console.WriteLine(p3 == p4);//True
  14. Console.WriteLine(p3.Equals(p4));//True
  15. // ReferenceEquals 方法用于对象的引用是否相等
  16. // ReferenceEquals 不能重写 注意
  17. Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
  18. p3 = p4;
  19. Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True
  20.  
  21. List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
  22. var result1 = list1.Where(M => M == new Point(, ));
  23. List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ) };
  24. var result2 = list2.Where(M => M == new TestPoint(, ));

完整代码

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. /// <summary>
  10. /// 定义一个点
  11. /// </summary>
  12. class Point
  13. {
  14. public int x { get; set; }
  15. public int y { get; set; }
  16. public Point(int x, int y)
  17. {
  18. this.x = x;
  19. this.y = y;
  20. }
  21. }
  22.  
  23. /// <summary>
  24. /// 定义一个点,并重写对象与对象是否相等的方法
  25. /// 可用于判断对象是否相等
  26. /// eg:
  27. /// obj1 == obj2
  28. /// obj1.Equals(obj2)
  29. /// </summary>
  30. class TestPoint : IEquatable<TestPoint>
  31. {
  32. public int x { get; set; }
  33. public int y { get; set; }
  34. public TestPoint(int x, int y)
  35. {
  36. this.x = x;
  37. this.y = y;
  38. }
  39.  
  40. /// <summary>
  41. /// 重载 == 运算符
  42. /// </summary>
  43. /// <param name="p1"></param>
  44. /// <param name="p2"></param>
  45. /// <returns></returns>
  46. public static bool operator ==(TestPoint p1, TestPoint p2)
  47. {
  48. return (p1.x == p2.x) && (p1.y == p2.y);
  49. }
  50.  
  51. /// <summary>
  52. /// 重载 != 运算符
  53. /// </summary>
  54. /// <param name="p1"></param>
  55. /// <param name="p2"></param>
  56. /// <returns></returns>
  57. public static bool operator !=(TestPoint p1, TestPoint p2)
  58. {
  59. return (p1.x != p2.x) || (p1.y != p2.y);
  60. }
  61.  
  62. /// <summary>
  63. /// 重写Equals(object obj)
  64. /// </summary>
  65. /// <param name="obj"></param>
  66. /// <returns></returns>
  67. public override bool Equals(object obj)
  68. {
  69. return this.Equals(obj as TestPoint);
  70. }
  71.  
  72. /// <summary>
  73. /// 重写 计算对象的哈希值方法(自定义 这里只是示范)
  74. /// </summary>
  75. /// <returns></returns>
  76. public override int GetHashCode()
  77. {
  78. return this.x.GetHashCode() + this.y.GetHashCode();
  79. }
  80.  
  81. /// <summary>
  82. /// 继承定义Equals<T>方法
  83. /// 需要继承接口IEquatable<T>
  84. /// </summary>
  85. /// <param name="other"></param>
  86. /// <returns></returns>
  87. public bool Equals(TestPoint other)
  88. {
  89. return (this.x == other.x) && (this.y == other.y);
  90. }
  91.  
  92. }
  93. class Program
  94. {
  95. static void Main(string[] args)
  96. {
  97. Point p1 = new Point(, );
  98. Point p2 = new Point(, );
  99. Console.WriteLine(p1 == p2);//False
  100. Console.WriteLine(p1.Equals(p2));//False
  101. // ReferenceEquals 方法用于对象的引用是否相等
  102. // ReferenceEquals 不能重写 注意
  103. Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
  104. p1 = p2;
  105. Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True
  106.  
  107. TestPoint p3 = new TestPoint(, );
  108. TestPoint p4 = new TestPoint(, );
  109. Console.WriteLine(p3 == p4);//True
  110. Console.WriteLine(p3.Equals(p4));//True
  111. // ReferenceEquals 方法用于对象的引用是否相等
  112. // ReferenceEquals 不能重写 注意
  113. Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
  114. p3 = p4;
  115. Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True
  116.  
  117. List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
  118. var result1 = list1.Where(M => M == new Point(, ));
  119. List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ) };
  120. var result2 = list2.Where(M => M == new TestPoint(, ));
  121.  
  122. Console.Read();
  123. }
  124. }
  125. }

ReferenceEquals 不能重写 注意

用于工作记录

2018年12月7日13:22:13

lxp

C# 对象对比是否相等 工作笔记的更多相关文章

  1. 2016年第2周读书笔记与工作笔记 scrollIntoView()与datalist元素

    这一周主要是看了html5网页开发实例与javascript 高级程序设计,供以后翻阅查找.  html5网页开发实例第1章与第二章的2.1部分: 第1章内容: html5在w3c的发展史. 浏览器的 ...

  2. javascript - 工作笔记 (事件四)

    在javascript - 工作笔记 (事件绑定二)篇中,我将事件的方法做了简单的包装,  JavaScript Code  12345   yx.bind(item, "click&quo ...

  3. 工作笔记3.手把手教你搭建SSH(struts2+hibernate+spring)环境

    上文中我们介绍<工作笔记2.软件开发经常使用工具> 从今天開始本文将教大家怎样进行开发?本文以搭建SSH(struts2+hibernate+spring)框架为例,共分为3步: 1)3个 ...

  4. python学习Day14 带参装饰器、可迭代对象、迭代器对象、for 迭代器工作原理、枚举对象、生成器

    复习 函数的嵌套定义:在函数内部定义另一个函数 闭包:被嵌套的函数 -- 1.外层通过形参给内层函数传参 -- 2.返回内部函数对象---->  延迟执行, 开放封闭原则: 功能可以拓展,但源代 ...

  5. 关于Java 中Integer 和Long对象 对比的陷阱(简单却容易犯的错误)

    彩票客户端“忘记密码”功能有bug,今天调试时,发现了原因: 功能模块中有一段: if(userpo.getId()!=Long.valueOf(uid)){ throw new VerifyExce ...

  6. Sencha Touch2 工作笔记

    Sencha Touch2 工作笔记 Ext.dataview.List activate( this, newActiveItem, oldActiveItem, eOpts ) Fires whe ...

  7. 工作笔记5.JAVA图片验证码

    本文主要内容为:利用JAVA图片制作验证码. 设计思路: 1.拷贝AuthImageServlet.class图片验证码 2.配置web.xml 3.JSP中,调用封装好的AuthImageServl ...

  8. 读书笔记——《MySQL DBA 工作笔记》

    关于前言 作者在前言中提出的一些观点很具有参考价值, 梳理完整的知识体系 这是每一个技术流都应该追逐的,完整的知识体系能够使我们对知识的掌握更加全面,而不仅仅局限于点 建立技术连接的思维,面对需求,永 ...

  9. 《工作笔记:移动web页面前端开发总结》

    工作笔记:移动web页面前端开发总结 移动web在当今的发展速度是一日千里,作为移动领域的门外汉,在这段时间的接触后,发现前端开发这一块做一个小小的总结. 1.四大浏览器内核 1.Trident (I ...

随机推荐

  1. Linux - mysql 异常:登录不上mysql数据库

    问题描述 重启虚拟机之后,用命令 mysql -u root -p 登录不上 mysql 数据库,页面显示: 但是,用命令 service mysqld status 可以查看状态 解决方案 1.查看 ...

  2. C# asp.net 配置文件连接sql 数据库

    先引用 using System.Configuration;//配置文件using System.Data.SqlClient; 我这里使用的是SqlServer 2008  sa 用户 密码也为s ...

  3. 巨杉内核笔记(一)| SequoiaDB 会话(session)简介

    SequoiaDB 会话(session)简介 会话(Session)的基本概念 容易弄混淆的两个概念是会话与连接. 通俗来讲,会话(Session) 是通信双方从开始通信到通信结束期间的一个上下文( ...

  4. 如何面试QA(面试官角度)

    面试是一对一 或者多对一的沟通,是和候选人 互相交换信息.平等的. 面试的目标是选择和雇佣最适合的人选.是为了完成组织目标.协助人力判断候选人是否合适空缺职位. 面试类型: (1)预判面试(查看简历后 ...

  5. NotePad++中如何改变光标样式(转换横着和竖着)?

    在键盘上找 Insert ,按这个Insert就可以把横向闪烁光标( _ )修改成竖向闪烁光标样式( | )

  6. C++——类与对象

    1.抽象: 是对具体对象(问题)进行概括,抽出这一类对象的公共性质并加以描述的过程. 1.1 先注意问题的本质描述,其次是实现过程和细节: 1.2 数据抽象:描述某类对象的属性或状态(对象相互区别的物 ...

  7. python使用临时文件

    # 需求 # 某项目中,我们从传感器中采集数据,没采集1G数据后,做数据分析,最终只保存分析结果 # 这样很大的临时文件如果常驻在内存,将消耗大量地内存资源,我们可以使用临时文件储存(外部储存) # ...

  8. Markdown进阶教程

      Markdown是很好用的轻量级标记语言,许多开发人员喜欢使用Markdown来记录学习心得和写博客.本篇博客主要介绍Markdown的高级技巧教程,Markdown的基础教程已经在上篇介绍过了. ...

  9. C语言输入一个整数转化为字符串

    将数字转化为对应的字符,可以通过n%10+48来实现,也可以通过n%10+'0'来实现,因为‘0’的ASCII码的数值就是48 因为字符串‘0’ 对应的10进制 整数是48 字符串'9'对应的10进制 ...

  10. python3练习100题——023

    再做一道,把这周的任务搞定- 其实看到这道题,很熟悉,让我想起大一时被C语言支配的恐惧.那个时候不停的在push自己,给自己很大的压力.上C语言课的时候让人昏昏欲睡,但是还是逼迫自己打起精神来学习,一 ...