C#中如何找到,如果两个对象相等 [英] c# How to find if two objects are equal

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

问题描述

我需要知道比较两个对象的最佳方式,如果有等于找出来。我重写这两个Gethash code和平等。因此,一个基本的类如下:

I'm needing to know the best way to compare two objects and to find out if there 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真实事件虽然这两个对象是不同的。我知道这是因为字符串1和字符串在每个对象相同的值,因此散列时将取消对方出。

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?

推荐答案

您当前的平等法被打破 - 还有比可能哈希值codeS更多的价值。这是完全合理的(和预期),你会偶尔有哪些是不相等的值,但给出相同的哈希值。等于应检查的实际值的:

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

有几件事情需要注意:

A few things to note:


  • 您生成哈希code将给予相同的值对于固定如果字符串1 字符串2 是相同的;它也将炸毁,如果字符串1 字符串2 为空。这是使用XOR散列的一个不幸的方面。我preFER是这样的:

  • 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&lt;试验&GT;

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

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