c#如何判断两个对象是否相等 [英] c# How to find if two objects are equal

查看:32
本文介绍了c#如何判断两个对象是否相等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道比较两个对象并确定它们是否相等的最佳方法.我覆盖了 GethashCode 和 Equals.所以一个基本的类看起来像:

I want to know the best way to compare two objects and to find out if they're equal. I'm overriding both GethashCode and Equals. So a basic class looks like:

public class Test
{
    public int Value { get; set; }
    public string String1 { get; set; }
    public string String2 { get; set; }

    public override int GetHashCode()
    {
        return Value ^ String1.GetHashCode() ^ String2.GetHashCode();
    }

    public override bool Equals( object obj )
    {
        return GetHashCode() == obj.GetHashCode();
    }
}

所以为了测试目的,我创建了两个对象:

So for testing purposes I created two objects:

Test t = new Test()
{
    Value = 1,
    String1 ="One",
    String2 = "One"
};

Test t2 = new Test()
{
    Value = 1,
    String1 = "Two",
    String2 = "Two"
};

bool areEqual = t.Equals( t2 );

在测试中,尽管两个对象不同,但这个 areEqual 返回 true 事件.我意识到这是因为 String1 和 String2 在每个对象中都是相同的值,因此在散列时会相互抵消.

In testing this areEqual returns true event though both objects are different. I realise this is because String1 and String2 are the same value in each object and thus cancels each other out when hashing.

有没有更好的方法来散列对象,我拥有的方法可以解决我的问题?

Is there a better way off hashing object that the method I have that will resolve my issue?

推荐答案

您当前的相等方法已损坏 - 值比可能的哈希码多.偶尔会有不相等但给出相同散列的值是完全合理的(并且是预期的).等于应该检查实际值:

Your current equality method is broken - there are more values than possible hash codes. It's entirely reasonable (and expected) that you will occasionally have values which are unequal but give the same hash. Equals should check the actual values:

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;
}

需要注意的几点:

  • 如果String1String2 相同,您生成哈希码的方式将为任何固定的Value 提供相同的值;如果 String1String2 为空,它也会爆炸.这是使用 XOR 进行散列的一个不幸方面.我更喜欢这样的东西:

  • Your way of generating the hashcode will give the same value for any fixed Value if String1 and String2 are the same; it will also blow up if String1 or String2 is null. This is an unfortunate aspect of using XOR for hashing. I prefer something like this:

// Put this extension method in a utility class somewhere
public static int SafeGetHashCode<T>(this T value) where T : class
{
    return value == null ? 0 : value.GetHashCode();
}

// and this in your actual class
public override int GetHashCode()
{
    int hash = 19;
    hash = hash * 31 + Value;
    hash = hash * 31 + String1.SafeGetHashCode();
    hash = hash * 31 + String2.SafeGetHashCode();
    return hash;
}

  • 一般来说,当涉及到继承时,相等会变得棘手.您可能需要考虑封闭您的班级.

  • Generally speaking, equality becomes tricky when inheritance gets involved. You may want to consider sealing your class.

    您可能还想实现 IEquatable

    这篇关于c#如何判断两个对象是否相等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆