简体   繁体   English

IEqualityComparer中令人费解的行为 <T> ,未命中Equals()方法中的断点

[英]Puzzling behaviour in IEqualityComparer<T>, breakpoint in the Equals() method is not hit

VS2013, .Net 4.5.2. VS2013,.Net 4.5.2。

Given this code 给定此代码

            var opCollectionA = new List<Option>() 
            {
              ...some elements added here..
            };

            var opCollectionB = new List<Option>() 
            {
              ...some elements added here..
            };

            var InAandB = opCollectionA.Except(opCollectionB, new OptionComparer()).ToArray();

and this comparer class: 和这个比较器类:

public class OptionComparer : EqualityComparer<Option>
{

    public override bool Equals(Option x, Option y)
    {
        //Check whether the objects are the same object. 
        if (Object.ReferenceEquals(x, y)) return true;
        //Check whether the options have the same Entry value. 
        return x != null && y != null && x.OptionEntry.Equals(y.OptionEntry);
    }

    public override int GetHashCode(Option obj)
    {
        //Get hash code for the OptionSection field if it is not null. 
        int hashOptionSection = obj.OptionSection == null ? 0 : obj.OptionSection.GetHashCode();

        //Get hash code for the OptionEntry field. 
        int hashOptionEntry = obj.OptionEntry.GetHashCode();

        //Calculate the hash code for the Option. 
        return hashOptionSection ^ hashOptionEntry;
    }
}

I am finding in Debug mode that GetHashcode is being called for each comparison but never Equals. 我发现在调试模式下,每次比较都会调用GetHashcode,但从不等于。 Consequently my comparer doesn't give the desired outcome. 因此,我的比较器没有给出期望的结果。

Can anyone explain this? 谁能解释一下?

TIA. TIA。

A hash code is used to speed up comparisons. 哈希码用于加速比较。 So a GetHashCode() implementation should be faster than Equals . 因此, GetHashCode()实现应该比Equals更快。

GetHashCode() must return equal hash codes for equal objects (where "equal" means that Equals() would return true ). GetHashCode() 必须相等的对象返回相等的哈希码(其中“ equal”表示Equals()将返回true )。

Since there are not enough int values to have a different hash code for each different object (for example strings), there will of course be different objects sharing the same hash code. 由于没有足够的int值来为每个不同的对象(例如字符串)使用不同的哈希码,因此当然会有不同的对象共享相同的哈希码。

If GetHashCode() returns the same hash code for two objects, Equals() must be called to decide if they are really equal. 如果GetHashCode()为两个对象返回相同的哈希码,则必须调用Equals()来确定它们是否真正相等。 But if GetHashCode() returns different values, you already know that these objects cannot be equal and don't have to call the - normally much more expensive - Equals() method. 但是,如果GetHashCode()返回不同的值,则您已经知道这些对象不能相等,并且不必调用通常通常更昂贵的Equals()方法。

CONCLUSION: 结论:

In your code you seem to expect two Option s to be equal if their OptionEntry is equal. 在您的代码中,如果两个OptionEntry相等,则似乎期望两个Option相等。 So the easiest way to fix your problem is to change your GetHashCode() so that it simply returns the hash code of OptionEntry : 因此,解决问题的最简单方法是更改GetHashCode() ,使其仅返回OptionEntry的哈希码:

public override int GetHashCode(Option obj)
{
    return obj.OptionEntry.GetHashCode();
}

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

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