Why doesn't the same thing work the same way when objects are passed to an object?

when I throw int and float on an object and compare them, equality is always false. Why?

float f = 0.0f; int i = 0; Console.WriteLine(f.Equals(i)); // true Console.WriteLine(i.Equals(f)); // false Console.WriteLine(i == f); // true Console.WriteLine("----------------"); object obf = f; object obi = i; Console.WriteLine(obf.Equals(obi)); // false Console.WriteLine(obi.Equals(obf)); // false Console.WriteLine(obi == obf); // false Console.WriteLine("----------------"); 

Update: this does NOT apply to the same type

  int i1 = 1; int i2 = 1; object oi1 = i1; object oi2 = i2; Console.WriteLine(oi1.Equals(oi2)); // true Console.WriteLine(oi2.Equals(oi1)); // true 
+6
source share
8 answers

A float equals only another float , and int equals only another int . The only lines that return true are the following:

 Console.WriteLine(f.Equals(i)); Console.WriteLine(i == f); 

In both cases, there is an implicit conversion of the value of i to a float , so they are equivalent:

 Console.WriteLine(f.Equals((float) i)); Console.WriteLine((float) i == f); 

These transformations are just normal transformations needed to resolve method and operator overloads.

None of the remaining lines include an implicit conversion, so they compare two different types, which gives a false result even when compared by value (which is the case with all Equals ). Therefore, the use of Equals in int fields returns true , because it is a comparison of two values ​​of the same type by value.

In this case:

 Console.WriteLine(obi == obf); 

he doesn't even try to compare numerical values ​​- he compares links for boxed objects. Since there are two different links, the result is false - and it will be even if both values ​​are of type int .

+10
source

Others have already explained why == does not work properly on your objects.

Regarding your editing: oi1.Equals(oi2) works because Equals is a virtual function and thus Int32.Equals(object) is called, whose return value is defined as follows:

true if obj is an Int32 instance and is equal to the value of this instance; otherwise false.

This also explains why obi.Equals(obf)) returns false: obf not an Int32 instance.

+3
source

You box int and float on an object, which means that they are compared as links. Since they are not references to the same object, they are not equal.

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

+1
source

When you declared two objects, they refer to another memory location:

 object obf = f; // this simplified as new float(f) object obi = i; // this simplified as new int(i) 

but try the following, let one object refer to another:

 obf = obi; Console.WriteLine(obf.Equals(obi)); 

MSDN, Object.Equals Method

The standard Equals implementation supports reference equality for reference types and bitwise equality for value types. Help equality means that references to objects that are compared refer to the same object. Bitwise equality means that the objects that are being compared are the same binary representation.

+1
source

To learn more about what happens when you enter value types, Shivprasad describes this quite briefly:

Boxing and Unboxing

Since you are boxing value types for objects, you are now doing reference equality. Since they are now in different memory locations in your example, they will return false.

+1
source

When you apply a value type to an object, it actually becomes a heap, and == compares the links at that point, which will be false. (Simplified)

0
source

Because Equals compares the address of the object reference and the two numbers are stored in different places. if you are comparing two types of values, then Equals behaves correctly, since it has been redefined for float, int, string and other types.

0
source

Because they are assigned to different memeory cells. 2 objects = only when they are the same object. In the float and int part, you get the true value when checking two variables, since the runtime checks their value. what all!

0
source

Source: https://habr.com/ru/post/900213/


All Articles