简体   繁体   English

我认为Object.Equals(Object,Object)支持按位相等而不是值相等

[英]I thought Object.Equals(Object, Object) support bitwise equality and not value equality

Static method Object.Equals(Object, Object) supports reference equality for reference types, and bitwise equality for value types, where with bitwise equality the objects that are compared have the same binary representation, while value equality objects compared have the same value even though they have different binary representations. 静态方法Object.Equals(Object, Object)支持引用类型的引用相等,以及值类型的按位相等,其中,按位相等,比较的对象具有相同的二进制表示,而比较的值相等对象具有相同的值,即使他们有不同的二进制表示。

For example, since i1 and b1 are of different types, they don't have the same binary representation and thus Object.Equals(Object, Object) returns false : 例如,由于i1b1属于不同类型,因此它们没有相同的二进制表示,因此Object.Equals(Object, Object)返回false

        int  i1 = 100;
        byte b1 = 100;
        Console.WriteLine(Object.Equals(i1, b1));//false

Object.Equals(Object, Object) should also return false when comparing d1 and d2 ( since the two variables have different binary representation of the same value ), but it instead returns true , which suggests that it compares them using value equality: 在比较d1d2Object.Equals(Object, Object)也应返回false(因为两个变量具有相同值的不同二进制表示),但它返回true ,这表明它使用值相等性来比较它们:

        decimal d1 = 1.10M;
        decimal d2 = 1.100M;
        Console.WriteLine(Object.Equals(d1, d2)); //true

Shouldn't Object.Equals(Object, Object) return False when comparing d1 and d2 ? 在比较d1d2Object.Equals(Object, Object)不应该返回False吗?

From http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx : 来自http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx

For example, consider two Decimal objects that represent the numbers 1.10 and 1.1000. 例如,考虑两个表示数字1.10和1.1000的Decimal对象。 The Decimal objects do not have bitwise equality because they have different binary representations to account for the different number of trailing zeroes. Decimal对象没有按位相等,因为它们具有不同的二进制表示以说明不同数量的尾随零。

thanx 感谢名单

Decimal is a value type and Equals method actually compares all its fields using Reflection. Decimal是值类型,Equals方法实际上使用Reflection比较其所有字段。 For more details, please refer to the MSDN: 有关更多详细信息,请参阅MSDN:

ValueType.Equals Method ValueType.Equals方法

Finally, your scope from the MSDN is incomplete. 最后,MSDN的范围不完整。 Here it is: 这里是:

For example, consider two Decimal objects that represent the numbers 1.10 and 1.1000. 例如,考虑两个表示数字1.10和1.1000的Decimal对象。 The Decimal objects do not have bitwise equality because they have different binary representations to account for the different number of trailing zeroes. Decimal对象没有按位相等,因为它们具有不同的二进制表示以说明不同数量的尾随零。 However, the objects have value equality because the numbers 1.10 and 1.1000 are considered equal for comparison purposes since the trailing zeroes are insignificant. 但是,对象具有值相等性,因为对于比较目的,数字1.10和1.1000被认为是相等的,因为尾随零是无关紧要的。

Object.Equals SHOULD implement value (not bitwise) equality. Object.Equals应该实现值(非按位)相等。

In the Decimal case, both objects are of the same type and the values are equal, so the result is true. 在Decimal情况下,两个对象的类型相同且值相等,因此结果为true。

In the int, byte case, objects are of different type, so the result is false. 在int,byte的情况下,对象的类型不同,因此结果为false。

You can look at the source of Object.Equals(Object, Object) with a tool like reflector. 您可以使用像reflector这样的工具查看Object.Equals(Object,Object)的来源。

Here's the source code of Object.Equals(Object, Object): 这是Object.Equals(Object,Object)的源代码:

public static bool Equals(object objA, object objB)
{
    return ((objA == objB) || (((objA != null) && (objB != null)) && objA.Equals(objB)));
}

Let's examine the clauses: 我们来看看这些条款:

(objA == objB) : This is the object equality operator which checks to see if these two objects reference the same object. (objA == objB) :这是对象相等运算符,它检查这两个对象是否引用同一个对象。 This clause is false for our case. 这个条款对我们来说是错误的。

(objA != null) && (objB != null) : This is true for our case. (objA != null) && (objB != null) :这适用于我们的情况。

objA.Equals(objB) : This is true (it delegates to Decimal.Equals(Object)) objA.Equals(objB) :这是真的(它委托给Decimal.Equals(Object))

We've got all true on the RHS of the || 我们在||的RHS上都是true operator, so the whole statement evaluates to true. 运算符,因此整个语句的计算结果为true。

From MSDN: 来自MSDN:

http://msdn.microsoft.com/en-us/library/w4hkze5k.aspx http://msdn.microsoft.com/en-us/library/w4hkze5k.aspx

Note that a derived type might override the Equals method to implement value equality. 请注意,派生类型可能会覆盖Equals方法以实现值相等。 Value equality means the compared objects have the same value but different binary representations. 值相等意味着比较的对象具有相同的值但具有不同的二进制表示。

Decimal definitely has an Equals override, as can be seen in the metadata. Decimal肯定有一个Equals覆盖,可以在元数据中看到。

Object.Equals(Object objA, Object objB) first does a quick reference check ( objA == objB ). Object.Equals(Object objA, Object objB)首先进行快速引用检查( objA == objB )。 If that fails, it tries to call the virtual Object.Equals(Object obj) which Decimal overrides to provide value equality. 如果失败,它会尝试调用Decimal重写的虚拟Object.Equals(Object obj)以提供值相等。

No way, bitwise equality does not make sense and it is never correct. 没有办法,按位相等没有意义,它永远不会正确。 Whatever is stored in whichever bitwise format, we do not care we care for actual business value. 无论以哪种按位格式存储,我们都不在乎我们关心实际的商业价值。

For business or science purpose 1.10 and 1.100 are same. 出于商业或科学目的,1.10和1.100是相同的。 Bitwise comparison means lexical comparison, which is wrong. 按位比较意味着词法比较,这是错误的。 "1.10" an "1.100" are different as they represent incorrect lexical sequence. “1.10”与“1.100”不同,因为它们表示不正确的词汇序列。

If you want to compare actual bits then you should use BitConverter.GetBytes which will give you actual bit sequence. 如果要比较实际位,则应使用BitConverter.GetBytes,它将为您提供实际的位序列。

Array.Compare( 
   BitConverter.GetBytes((decimal)1.10),
   BitConverter.GetBytes((decimal)1.100))

I don't know if there is an Array.Compare method or not but you can create one and hope you got the point. 我不知道是否有一个Array.Compare方法,但你可以创建一个,希望你明白这一点。

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

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