简体   繁体   English

使用哈希码概念困境进行对象比较

[英]Object compare using Hash Code-Concept dilemma

I have written a piece of code to check for Object Equality. 我已经编写了一段代码来检查对象相等性。 I took refrence from one of the question in stack overflow itself. 我从堆栈溢出本身中的一个问题中吸取了教训。 Now this code is giving true even if we have two different objects. 现在,即使我们有两个不同的对象,此代码也能实现。 Can someone explain why? 有人可以解释为什么吗?

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();

        }
    }
}

and in my UtilityClass 在我的UtilityClass

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

After no success,i tried below code which also return true. 没有成功之后,我尝试了下面的代码,这些代码也返回true。 What blunder am I doing here?Please help 我在这里犯什么错误请帮忙

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();

    }
}

You override Equals() and GetHashCode() to define what "equals" means in a given context. 您可以重写Equals()GetHashCode()来定义“等于”在给定上下文中的含义。

Your Equals of: Equals

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

Has a bug in that it should be if(test == null) return false; 有一个错误,应该是if(test == null) return false; but otherwise it says "two Test objects are the same if they have the same Value , String1 and String2 , otherwise they are not. Your GetHashCode() is consistent with that. 但除此之外,它说:“二Test对象是相同的,如果它们具有相同的ValueString1String2 ,否则他们是不会。你GetHashCode()是与一致。

As such, it's not a bug to have the code return true : 因此,使代码返回true并不是一个错误:

Now this code is giving true even if we have two different objects. 现在,即使我们有两个不同的对象,此代码也能实现。

Yes, two different equal objects. 是的,两个不同的相等对象。

If you want Equals and GetHashCode() to tell you whether they are the same object or not (that is, for an object of a class to be equal only to itself) then don't override Equals and GetHashCode at all; 如果您希望EqualsGetHashCode()告诉您它们是否是同一对象(即,一个类的对象仅与自身相等),则不要覆盖EqualsGetHashCode stay with the default behaviour. 保持默认行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM