簡體   English   中英

使用哈希碼概念困境進行對象比較

[英]Object compare using Hash Code-Concept dilemma

我已經編寫了一段代碼來檢查對象相等性。 我從堆棧溢出本身中的一個問題中吸取了教訓。 現在,即使我們有兩個不同的對象,此代碼也能實現。 有人可以解釋為什么嗎?

using System;
namespace ConsolePractice
{
    public class Test
    {
        public int Value { get; set; }
        public string String1 { get; set; }
        public string String2 { get; set; }



        public override int GetHashCode()
        {
            int hash = 19;
            hash = hash * 31 + Value;
            hash = hash * 31 + String1.SafeGetHashCode();
            hash = hash * 31 + String2.SafeGetHashCode();
            return hash;
        }
        public override bool Equals(object obj)
        {
            Test test = obj as Test;
            if (obj == null)
            {
                return false;
            }
            return Value == test.Value &&
                String1 == test.String1 &&
                String2 == test.String2;
        }
    }

    class Demo
    {
        static void Main()
        {
            Test p1 = new Test
            {
                Value = 10,
                String1 = "Test1",
                String2 = "Test2"
            };
            Test p2 = new Test
            {
                Value = 10,
                String1 = "Test1",
                String2 = "Test2"
            };
            bool areEqual = p1.Equals(p2);

            Console.WriteLine(areEqual.ToString());
            Console.ReadLine();

        }
    }
}

在我的UtilityClass

 static class utility
    {
        public static int SafeGetHashCode<T>(this T value) where T : class
        {
            return value == null ? 0 : value.GetHashCode();
        }
    }

沒有成功之后,我嘗試了下面的代碼,這些代碼也返回true。 我在這里犯什么錯誤請幫忙

using System;

using System.Collections.Generic;


class ThingEqualityComparer : IEqualityComparer<Thing>
{
    public bool Equals(Thing x, Thing y)
    {
        if (x == null || y == null)
            return false;

        return (x.Id == y.Id && x.Name == y.Name);
    }

    public int GetHashCode(Thing obj)
    {
        return obj.GetHashCode();
    }
}


public class Thing
{
    public int Id { get; set; }
    public string Name { get; set; }

}
class Demo
{
    static void Main()
    {
        Thing p1 = new Thing
        {
            Id = 10,
            Name = "Test1",

        };
        Thing p2 = new Thing
        {
            Id = 10,
            Name = "Test1",

        };

        var comparer = new ThingEqualityComparer();
        Console.WriteLine(comparer.Equals(p1, p2));


        Console.ReadLine();

    }
}

您可以重寫Equals()GetHashCode()來定義“等於”在給定上下文中的含義。

Equals

Test test = obj as Test;
if (obj == null)
{
  return false;
}
return Value == test.Value &&
  String1 == test.String1 &&
  String2 == test.String2;

有一個錯誤,應該是if(test == null) return false; 但除此之外,它說:“二Test對象是相同的,如果它們具有相同的ValueString1String2 ,否則他們是不會。你GetHashCode()是與一致。

因此,使代碼返回true並不是一個錯誤:

現在,即使我們有兩個不同的對象,此代碼也能實現。

是的,兩個不同的相等對象。

如果您希望EqualsGetHashCode()告訴您它們是否是同一對象(即,一個類的對象僅與自身相等),則不要覆蓋EqualsGetHashCode 保持默認行為。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM