Two .NET objects that are equal don't say they are

走远了吗. 提交于 2019-12-28 06:19:31

问题


I have the following code:

object val1 = 1;
object val2 = 1;

bool result1 = (val1 == val2);//Equals false
bool result2 = val1.Equals(val2); //Equals true

What's up with that? Is the only way to fix this to go with .Equals() method?


回答1:


The operator == is static, not virtual, so the behaviour is determined by the static type and not the runtime type. The default implementation for == on objects of reference type is to compare the references (although types can implement a different behaviour, for example string). You have two different objects and they don't have the same reference so == returns false.

The solution, as you point out, is to use Equals. Equals is a virtual method. Since value1 has runtime type Int32 you end up calling Int32.Equals. From .NET Reflector you can see that the implementation of this is as follows:

public override bool Equals(object obj)
{
    return ((obj is int) && (this == ((int) obj)));
}

In other words, it checks if the argument is of type int, and if so casts it and uses the == that is defined for int. This compares the values of the integers.

Is the only way to fix this to go with .Equals() method?

An alternative is to cast your objects to int and then use ==, just as the implementation of Int32.Equals does.




回答2:


Yes. == checks for reference equality. Use Equals where you want to compare content.

You might be wondering why this is so with objects. When you set an integer (value type) to an object variable, an operation called boxing happens. This operation wraps the value type into an object and puts it on the heap and returns a reference. This happens twice and references becomes different (although the values are the same).




回答3:


== checks whether the two objects are identical. They are not. They represent the same number, but are stored at different locations in memory.

It’s like comparing two apples. Both are apples and look the same, but they are different objects.




回答4:


That is because when you cast them to objects they are "converted" to references to int values. And the two references are not equal. But equals compares the referenced values instead of the references.




回答5:


If you aren't using object but a custom class, you can override the == and != operators, and should probably implement the IEqualityComparer<T> interface

public static bool operator ==(MyType left, MyType right)
{
    //code here, don't forget about NULLS when writing comparison code!!!
}

public static bool operator !=(MyType left, MyType right)
{
    return !(left == right);
}

public bool Equals(MyType x, MyType y)
{
    return (x == y);
}

public int GetHashCode(MyType obj)
{
    return base.GetHashCode();
}



回答6:


Two objects are equal if they point to the same space in memory.

val1 == val1; //Equals true

As pointed by tc you can make an operator overload.

public static bool operator ==(Object a, Object b)

This way the behavior of the operator == will be the one defined by this method.

You should also overload the operator != when you overload ==.




回答7:


The CIL for your code boxes the two integers and compares the two objects that result from the boxing (==). This comparison is by reference.

  .locals init ([0] object val1,
           [1] object val2,
           [2] bool result1,
           [3] bool result2)
  IL_0000:  nop
  IL_0001:  ldc.i4.1
  IL_0002:  box        [mscorlib]System.Int32
  IL_0007:  stloc.0
  IL_0008:  ldc.i4.1
  IL_0009:  box        [mscorlib]System.Int32
  IL_000e:  stloc.1
  IL_000f:  ldloc.0
  IL_0010:  ldloc.1
  IL_0011:  ceq
  IL_0013:  stloc.2
  IL_0014:  ldloc.0
  IL_0015:  ldloc.1
  IL_0016:  callvirt   instance bool [mscorlib]System.Object::Equals(object)
  IL_001b:  stloc.3

For the .Equals it calls Object.Equals, which calls Int32.Equals (virtual method call on Object):

public override bool Equals(object obj)
{
    return ((obj is int) && (this == ((int) obj)));
}

This casts to int and compares the values as integers, a value type comparison.



来源:https://stackoverflow.com/questions/3470145/two-net-objects-that-are-equal-dont-say-they-are

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!