简体   繁体   English

GetHashCode功能

[英]GetHashCode functionality

Why two object's hash code is not same even though they have similar values. 为什么两个对象的哈希码即使值相似但也不相同。 What is the other best approach to find value equality among objects with out reading ones each property and have check with other ones property? 在不读取每个属性的对象而检查其他属性的对象之间找到值相等的另一种最佳方法是什么?

                Person person = new Person();
                person.Name = "X";
                person.Age = 25;
                person.Zip = 600056;
                person.Sex = 'M';

                Person person1 = new Person();
                person1.Name = "X";
                person1.Age = 25;
                person1.Zip = 600056;
                person1.Sex = 'M';


                int hashCode1 = person1.Name.GetHashCode();
                int hashCode = person.Name.GetHashCode();

                // hashCode1 and hashCode values are same.

                if (person.GetHashCode() == person1.GetHashCode())
                {
                    // Condition is not satisfied
                }

in your code hashCode1 == hashCode is true because hashing same string will always give you the same result. 在您的代码中hashCode1 == hashCode为true,因为哈希相同的字符串将始终为您提供相同的结果。 however insances are different so you have to override GetHashCode() in a way that fits your business logic for example 但是,instances不同,因此您必须以适合您的业务逻辑的方式重写GetHashCode()

    public override int GetHashCode()
    {
        return Name.GetHashCode() ^ Zip.GetHashCode() ^ Sex ^ Age;
    }

I sugest you take a look at this answer GetHashCode Guidelines in C# . 我建议您看一下C#中的GetHashCode指南

and http://musingmarc.blogspot.com/2007/08/vtos-rtos-and-gethashcode-oh-my.html http://musingmarc.blogspot.com/2007/08/vtos-rtos-and-gethashcode-oh-my.html

In your example, both objects are alike but not the same. 在您的示例中,两个对象相似但不相同。 They are different instances, so they have different hash code. 它们是不同的实例,因此它们具有不同的哈希码。 Also person1 == person2 and person1.Equals(person2) are false. 另外person1 == person2和person1.Equals(person2)为假。

You can override this behavior. 您可以覆盖此行为。 If you consider the two objects are the same if those properties are equal, the you can: 如果您认为两个对象在属性相等的情况下是相同的,则可以:

public override bool Equals(object other) {
    if(other == this) return true;

    var person = other as Person;
    if(person == null) return false;

    return person.Name == Name && person.Age == Age && person.Zip == Zip && person.Sex == Sex;

} }

public override int GetHashCode() {
    //some logic to create the Hash Code based on the properties. i.e.
    return (Name + Age + Zip + Sex).GetHashCode(); // this is just a bad example!
}

Why two object's hash code is not same even though they have similar values 为什么两个对象的哈希码尽管值相似但也不相同

Because that's the expected way of identifying them uniquely. 因为这是唯一标识它们的预期方法。 AFAIK, most frameworks/libraries uses the pointer value for this by default. AFAIK,默认情况下,大多数框架/库为此使用指针值。

What is the other best approach to find value equality among objects with out reading ones each property and have check with other ones property? 在不读取每个属性的对象而检查其他属性的对象之间找到值相等的另一种最佳方法是什么?

Depends on what makes them "equal", according to your needs. 根据您的需要,取决于使它们“相等”的原因。 Basically it's still comparing properties, but maybe just more limited. 基本上,它仍在比较属性,但可能只是比较有限。

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

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