zoukankan      html  css  js  c++  java
  • C# 对象对比是否相等 工作笔记

    需要在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(1,1), new Point(1, 2), new Point(1, 3), new Point(1, 4), new Point(1, 5), new Point(1, 6)};
     var result1 = list1.Where(M => M == new Point(1, 3));

    三种对比方法均不能

    Point p1 = new Point(2, 1);
    Point p2 = new Point(2, 1);
    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(2, 1);
                Point p2 = new Point(2, 1);
                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(2, 1);
                TestPoint p4 = new TestPoint(2, 1);
                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(1,1), new Point(1, 2), new Point(1, 3), new Point(1, 4), new Point(1, 5), new Point(1, 6)};
                var result1 = list1.Where(M => M == new Point(1, 3));
                List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(1, 1), new TestPoint(1, 2), new TestPoint(1, 3), new TestPoint(1, 4), new TestPoint(1, 5), new TestPoint(1, 6) };
                var result2 = list2.Where(M => M == new TestPoint(1, 3));

    完整代码

    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(2, 1);
                Point p2 = new Point(2, 1);
                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(2, 1);
                TestPoint p4 = new TestPoint(2, 1);
                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(1,1), new Point(1, 2), new Point(1, 3), new Point(1, 4), new Point(1, 5), new Point(1, 6)};
                var result1 = list1.Where(M => M == new Point(1, 3));
                List<TestPoint> list2 = new List<TestPoint>() { new TestPoint(1, 1), new TestPoint(1, 2), new TestPoint(1, 3), new TestPoint(1, 4), new TestPoint(1, 5), new TestPoint(1, 6) };
                var result2 = list2.Where(M => M == new TestPoint(1, 3));
    
                Console.Read();
            }
        }
    }

    ReferenceEquals 不能重写 注意

    用于工作记录

    2018年12月7日13:22:13

    lxp

  • 相关阅读:
    Warning:detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd".
    Ubuntu16.04安装K8s步骤和踩坑记录【不错】
    修改主机名(/etc/hostname和/etc/hosts区别)
    Windows10内置Linux子系统初体验
    Docker Swarm常用命令
    Docker Swarm集群部署
    Docker容器CPU、memory资源限制
    Docker监控容器资源的占用情况
    修改Docker容器启动配置参数
    sort-colors——排序3种数字
  • 原文地址:https://www.cnblogs.com/lixipeng/p/10082382.html
Copyright © 2011-2022 走看看