Jose
Jose

Reputation: 11091

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

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?

Upvotes: 17

Views: 11536

Answers (7)

Simon D
Simon D

Reputation: 4290

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.

Upvotes: 0

Falle1234
Falle1234

Reputation: 5063

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.

Upvotes: 1

Andrey
Andrey

Reputation: 60055

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).

Upvotes: 10

Mark Byers
Mark Byers

Reputation: 837946

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.

Upvotes: 35

Daniel Moura
Daniel Moura

Reputation: 7966

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 ==.

Upvotes: 0

CaffGeek
CaffGeek

Reputation: 22054

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

Upvotes: 0

== 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.

Upvotes: 2

Related Questions