Int32.Equals vs'=='运算符 [英] Int32.Equals vs '==' operator

查看:58
本文介绍了Int32.Equals vs'=='运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,但之前似乎没有人问过这个问题,所以就去了.

I looked around but no one seems to have asked this question before, so here it goes.

我正在开发一个具有IEquatable接口的自定义类,因此,我正在制作自己的Equals方法.看起来是这样的:

I'm working on a custom class that will have the IEquatable interface, and as such I'm making my own Equals method. Here's what it looks like:

public bool Equals(QueryFilter qfilter)
    {
        if (qfilter == null)
        {
            return false;
        }
        return ((this.Value.Equals(qfilter.Value)) && 
            (this.Name.Equals(qfilter.Name)) &&
            (this.Order == qfilter.Order));
    }

其中Value,Name和Order是QueryFilter类的字段.值和名称是strings,但是Order是int,我想知道使用==运算符是否合适,或者是否应该使用Int32.Equals方法来匹配"其他字段的方式在进行比较吗?我检查了MSDN,但是并没有做太多说明,只是说它已超载,但是我不确定在这种情况下这意味着什么. ==会一直在工作吗?

Where Value, Name and Order are fields of the QueryFilter class. Value and Name are strings, but Order is an int, and I was wondering if using the == operator is fine, or if I should go for the Int32.Equals method, to "match" how the other fields are making their comparisons? I checked the MSDN but it doesn't elaborate much, only saying that it's overloaded, but I'm not sure what that means in this situation. That the == will be the one at work always?

因此,总而言之,哪个更好? Int32.Equals==?我什么时候应该使用每个?

So in conclusion, which one is better? Int32.Equals or ==? And when should I use each one?

推荐答案

是的,只要Order compile-time 类型int.

例如,如果编译时类型为object,那么您将处理 boxed int值,然后将这些box与引用相等进行比较,这不是您所需要的.再之后.但是只要编译器知道它们是int值,就可以了.

If the compile-time type were object for example, then you'd be dealing with boxed int values and comparing those boxes with reference equality, which isn't what you're after. But so long as the compiler knows they're int values, it's fine.

请注意,对于NameValue再次执行此操作 也很好-假设那些属性的 compile-time 类型为string (因为编译器将使用string提供的重载.然后,您可以使用以下事实:&&处于短路状态,摆脱不必要的括号,然后剩下:

Note that it's also fine to do this for Name and Value - again, assuming that the compile-time type of those properties is string (because then the compiler uses the overload provided by string. Then you can use the fact that && is short-circuiting, get rid of unnecessary brackets, and get left with:

public  bool Equals(QueryFilter other)
{
    return other != null &&
           this.Value == other.Value &&
           this.Name == other.Name &&
           this.Order == other.Order;
}

...我当然希望看到它.

... which I would certainly prefer to see.

这也以明显的方式处理了ValueNamenull的情况(空引用彼此相等,但不等于任何非空引用).如果现有代码达到this.Value.Equalsthis.Name.Equals的空属性值,则会抛出NullReferenceException. (很可能您要确保永远不会这样,但是值得一提.)

This also handles the case where Value or Name is null, in an obvious way (null references are equal to each other and not equal to any non-null references). Your existing code would throw a NullReferenceException if it reached this.Value.Equals or this.Name.Equals for a null property value. (It may well be that you ensure that's never the case, but it's worth being aware of.)

您还应该确保您的哈希码与等式一致,并且也应覆盖Equals(object).

You should also make sure that your hash code is consistent with equality, and override Equals(object) too.

这篇关于Int32.Equals vs'=='运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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