对象的比较 [英] Comparing objects

查看:126
本文介绍了对象的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类它包含了一些字符串的成员,一些双成员和部分数组对象。

I have a class it contains some string members, some double members and some array objects.

我创建这个类的两个对象,有没有比较这些对象,并说他们的平等的任何简单,有效的方法?有什么建议么?

I create two objects of this class, is there any simplest, efficient way of comparing these objects and say their equal? Any suggestions?

我知道如何写一个比较功能,但它会很耗时。

I know how to write a compare function, but will it be time consuming.

推荐答案

您可以真正做到这一点的唯一方法是重写布尔的Object.Equals(对象除外) 在满足你的平等条件,返回true,并返回否则为false。您还必须重写 INT Object.GetHashCode()来从返回的所有的,你重写时要考虑数据的计算一个int 等于()

The only way you can really do this is to override bool Object.Equals(object other) to return true when your conditions for equality are met, and return false otherwise. You must also override int Object.GetHashCode() to return an int computed from all of the data that you consider when overriding Equals().

顺便说一句,请注意合约的GetHashCode()指定返回值必须是平等的两个对象时,<​​code>等于()进行比较时,他们将返回true。这意味着返回0; 是一个有效的实施的GetHashCode(),但它会导致效率低下,当你的类的对象作为字典键,或存储在的HashSet< T>

As an aside, note that the contract for GetHashCode() specifies that the return value must be equal for two objects when Equals() would return true when comparing them. This means that return 0; is a valid implementation of GetHashCode() but it will cause inefficiencies when objects of your class are used as dictionary keys, or stored in a HashSet<T>.

我实现平等的方法是像这样的:

The way I implement equality is like this:

public class Foo : IEquatable<Foo>
{
    public bool Equals(Foo other)
    {
        if (other == null)
            return false;

        if (other == this)
            return true; // Same object reference.

        // Compare this to other and return true/false as appropriate.
    }

    public override bool Equals(Object other)
    {
        return Equals(other as Foo);
    }

    public override int GetHashCode()
    {
        // Compute and return hash code.
    }
}



实施的一种简单方法 GetHashCode的()是你考虑的平等数据的哈希码XOR一起等于()。所以,如果,例如,你比较平等的性质是字符串名字;字符串姓氏; INT标识; ,您的实现可能是这样的:

A simple way of implementing GetHashCode() is to XOR together the hash codes of all of the data you consider for equality in Equals(). So if, for example, the properties you compare for equality are string FirstName; string LastName; int Id;, your implementation might look like:

public override int GetHashCode()
{
    return (FirstName != null ? FirstName.GetHashCode() : 0) ^
        (LastName != null ? LastName.GetHashCode() : 0) ^
        Id; // Primitives of <= 4 bytes are their own hash codes
}



我通常不要替换平等的运营商,因为大部分的时间,我很担心只对字典的键或集合的目的平等。我只会考虑重写相等运算符,如果你有可能通过值比参考做多比较,因为它在语法上是更简洁。但是,你要记住改变,你用 == != 的对象(包括所有地方你的实施的equals()!)使用 Object.ReferenceEquals(),或两个操作数转换为对象。这讨厌的疑难杂症(可引起无限递归在平等的测试,如果你不小心)是主要的原因,我很少覆盖这些运营商之一。

I typically do not override the equality operators, as most of the time I'm concerned with equality only for the purposes of dictionary keys or collections. I would only consider overriding the equality operators if you are likely to do more comparisons by value than by reference, as it is syntactically less verbose. However, you have to remember to change all places where you use == or != on your object (including in your implementation of Equals()!) to use Object.ReferenceEquals(), or to cast both operands to object. This nasty gotcha (which can cause infinite recursion in your equality test if you are not careful) is one of the primary reasons I rarely override these operators.

这篇关于对象的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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