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

/// <summary>
/// 定义一个点
/// </summary>
class Point
{
public int x { get; set; }
public int y { get; set; }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
 List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
var result1 = list1.Where(M => M == new Point(, ));

三种对比方法均不能

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

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

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

   /// <summary>
/// 定义一个点,并重写对象与对象是否相等的方法
/// 可用于判断对象是否相等
/// eg:
/// obj1 == obj2
/// obj1.Equals(obj2)
/// </summary>
class TestPoint : IEquatable<TestPoint>
{
public int x { get; set; }
public int y { get; set; }
public TestPoint(int x, int y)
{
this.x = x;
this.y = y;
} /// <summary>
/// 重载 == 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator ==(TestPoint p1, TestPoint p2)
{
return (p1.x == p2.x) && (p1.y == p2.y);
} /// <summary>
/// 重载 != 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator !=(TestPoint p1, TestPoint p2)
{
return (p1.x != p2.x) || (p1.y != p2.y);
} /// <summary>
/// 重写Equals(object obj)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
return this.Equals(obj as TestPoint);
} /// <summary>
/// 重写 计算对象的哈希值方法(自定义 这里只是示范)
     /// 该方法用于判断对象的哈希值是否相等 如对象哈希值相同 就认为两个对象 相等
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
} /// <summary>
/// 继承定义Equals<T>方法
/// 需要继承接口IEquatable<T>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(TestPoint other)
{
return (this.x == other.x) && (this.y == other.y);
} }

使用大概示范

       Point p1 = new Point(, );
Point p2 = new Point(, );
Console.WriteLine(p1 == p2);//False
Console.WriteLine(p1.Equals(p2));//False
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
p1 = p2;
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True TestPoint p3 = new TestPoint(, );
TestPoint p4 = new TestPoint(, );
Console.WriteLine(p3 == p4);//True
Console.WriteLine(p3.Equals(p4));//True
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
p3 = p4;
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
var result1 = list1.Where(M => M == new Point(, ));
List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ) };
var result2 = list2.Where(M => M == new TestPoint(, ));

完整代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication1
{
/// <summary>
/// 定义一个点
/// </summary>
class Point
{
public int x { get; set; }
public int y { get; set; }
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
} /// <summary>
/// 定义一个点,并重写对象与对象是否相等的方法
/// 可用于判断对象是否相等
/// eg:
/// obj1 == obj2
/// obj1.Equals(obj2)
/// </summary>
class TestPoint : IEquatable<TestPoint>
{
public int x { get; set; }
public int y { get; set; }
public TestPoint(int x, int y)
{
this.x = x;
this.y = y;
} /// <summary>
/// 重载 == 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator ==(TestPoint p1, TestPoint p2)
{
return (p1.x == p2.x) && (p1.y == p2.y);
} /// <summary>
/// 重载 != 运算符
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static bool operator !=(TestPoint p1, TestPoint p2)
{
return (p1.x != p2.x) || (p1.y != p2.y);
} /// <summary>
/// 重写Equals(object obj)
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
return this.Equals(obj as TestPoint);
} /// <summary>
/// 重写 计算对象的哈希值方法(自定义 这里只是示范)
/// </summary>
/// <returns></returns>
public override int GetHashCode()
{
return this.x.GetHashCode() + this.y.GetHashCode();
} /// <summary>
/// 继承定义Equals<T>方法
/// 需要继承接口IEquatable<T>
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool Equals(TestPoint other)
{
return (this.x == other.x) && (this.y == other.y);
} }
class Program
{
static void Main(string[] args)
{
Point p1 = new Point(, );
Point p2 = new Point(, );
Console.WriteLine(p1 == p2);//False
Console.WriteLine(p1.Equals(p2));//False
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//False
p1 = p2;
Console.WriteLine(System.Object.ReferenceEquals(p1, p2));//True TestPoint p3 = new TestPoint(, );
TestPoint p4 = new TestPoint(, );
Console.WriteLine(p3 == p4);//True
Console.WriteLine(p3.Equals(p4));//True
// ReferenceEquals 方法用于对象的引用是否相等
// ReferenceEquals 不能重写 注意
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//False
p3 = p4;
Console.WriteLine(System.Object.ReferenceEquals(p3, p4));//True List<Point> list1 = new List<Point>() { new Point(,), new Point(, ), new Point(, ), new Point(, ), new Point(, ), new Point(, )};
var result1 = list1.Where(M => M == new Point(, ));
List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ), new TestPoint(, ) };
var result2 = list2.Where(M => M == new TestPoint(, )); Console.Read();
}
}
}

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 -初体验笔记

    课堂笔记 鸟哥Linux私房菜 Linux 版本很多,内核都是一样的 计算机基础知识: 1.完整计算机系统:软件+硬件 硬件:物理装置本身,计算机的物质基础 软件:相对硬件而言, 程序:计算机完成一项 ...

  2. c#从前台界面找后台方法

    比如你新接触一个项目  项目别人已经写的差不多了  你对项目一无所知,别人安排给你活  怎么最快速度找到你要干的活对应的东西 以谷歌浏览器为例 一个项目你要修改  库存信息列表 右键检查或者F12 找 ...

  3. 安装 centos8.1

    阿里云镜像下载链接 http://mirrors.aliyun.com/centos/8.1.1911/isos/x86_64/ 选择 CentOS-8.1.1911-x86_64-dvd1.iso ...

  4. Failed to restart docker.service: Unit not found 镜像加速

    解决方案 以前的安装残留 重新安装 find / -name "docker*" centos8 添加软件源信息 yum-config-manager --add-repo htt ...

  5. HashMap源码(一)

    本文主要是从学习的角度看HashMap源码 HashMap的数据结构 HashMap是一个数组+链表的结构(链表散列),每个节点在HashMap中以一个Node存在: HashMap的初始化 publ ...

  6. python爬虫模拟登录的图片验证码处理和会话维持

    目标网站:古诗文网 登录界面显示: 打开控制台工具,输入账号密码,在ALL栏目中进行抓包 数据如下: 登录请求的url和请求方式 登录所需参数 参数分析: __VIEWSTATE和__VIEWSTAT ...

  7. JS高级---递归案例

    递归案例     递归案例: 求一个数字各个位数上的数字的和:  123   --->6 ---1+2+3 //递归案例:求一个数字各个位数上的数字的和: 123 --->6 ---1+2 ...

  8. Servlet相关配置

    配置方式 webXML 定义标签<urlpartten>Servlet访问路径 注解 定义的<urlpartten>数组:可以为一个servlet定义多个访问路径. packa ...

  9. Codeforces Round #598 (Div. 3) B Minimize the Permutation

    B. Minimize the Permutation You are given a permutation of length nn. Recall that the permutation is ...

  10. mysql 实践(例题)

    MySQL安装见本博 安装成功后,开始菜单中找到 “MySQL 8.0 Command Line Client” 进行启动(启动后,可直接输入MySQL密码) 1. create database 数 ...