What is the difference between using the == operator and the Equals method on a boxer type?

Given these two statements ...

((object)false) == ((object)false) ((object)false).Equals((object)false) 

The first statement returns false. The second statement returns true.

I understand why the first statement returns false - when a logical field is placed in a box, it becomes a reference type, and the two links are not equal. But why / how does the second statement lead to true?

+6
c # boxing
source share
6 answers

Because it still calls the polymorphic Equals method, basically.

Sample code to demonstrate another type:

 using System; struct Foo { public override bool Equals(object other) { Console.WriteLine("Foo.Equals called!"); return true; } public override int GetHashCode() { return 1; } } class Program { static void Main(string[] args) { object first = new Foo(); object second = new Foo(); first.Equals(second); } } 

It still prints "Foo.Equals"! because calling the Equals method on the "field" still calls Foo.Equals .

Now == not overridden, it is overloaded ... so if you write:

 object first = ...; object second = ...; bool same = first == second; 

This will always be compared for a reference identifier, without any code of any type.

+8
source share
+5
source share

As you said, the first example checks if the links are equal, and the second checks the same values ​​for each object.

From MSDN :

The following statements should be true for all implementations of the Equals method. In the list, x, y, and z are references to objects that are not null.

x.Equals (x) returns true, with the exception of floating point types. See IEC 60559: 1989, Binary floating point arithmetic for microprocessor systems.

x.Equals (y) returns the same value as y.Equals (x).

x.Equals (y) returns true if both x and y are NaN.

If (x.Equals (y) & y.Equals (z)) returns true, then x.Equals (z) returns true.

Successive calls to x.Equals (y) return the same value if the objects referenced by x and y are not modified.

x.Equals (null) returns false.

+1
source share

Operator overloading is not polymorphic, but Equals is. Despite the fact that bool has overloaded ==, casting it to object , you use an object implementation that compares reference equality. But you are still using the bool Equals version.

+1
source share

The Equals method is a virtual method that is overridden by the Boolean type. Therefore, it does not matter that you put bool on object in the second line; it still uses the vtable type to find the Equals implementation provided by the actual object type (this polymorphism is for you!).

The == operator is a static operator , so the corresponding overload is selected at compile time. The compiler sees that you are comparing two objects of type object with this operator and therefore choose overloading (object, object) .

Here is a silly little program to illustrate the difference:

 class Thing { public virtual void AnnounceSelf() { Console.WriteLine("I am a Thing."); } public static void AnnounceThing(Thing other) { Console.WriteLine("Here is a Thing."); } public static void AnnounceThing(OtherThing other) { Console.WriteLine("Here is ANOTHER type of Thing."); } } class OtherThing : Thing { public override void AnnounceSelf() { Console.WriteLine("I am ANOTHER Thing."); } } class Program { public static void Main() { Thing t = new Thing(); // Outputs "I am a Thing." as expected. t.AnnounceSelf(); // Outputs "Here is a Thing." as expected. Thing.AnnounceThing(t); t = new OtherThing(); // This method is virtual, so even though t is typed as Thing, // the implementation provided by OtherThing will be called; // outputs "I am ANOTHER Thing." t.AnnounceSelf(); // In contrast to above, this method will NOT call the more // specific overload of AnnounceThing (accepting an OtherThing // argument) because t is only typed as Thing, so the compiler // will go with the first; // outputs "Here is a Thing." Thing.AnnounceThing(t); // THIS will output "Here is ANOTHER type of Thing." Thing.AnnounceThing((OtherThing)t); } } 
+1
source share

1st for links, second for values!

-one
source share

All Articles