1. 程式人生 > >C# 物件對比是否相等 工作筆記

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