See the following test code:
Object a = 11;
Object B = 11;
Console. WriteLine (a = B); // output false
In actual development, it may not be so intuitive. For example, in the following scenario, we need to check whether the fields in Datarow are equal. You may write as follows:
DataRow hbrow = needAddRows. Where (p => p ["dh"] = dr ["dh"]). FirstOrDefault (); // dr is a datarow. Congratulations! You won the prize! :) Hbrow is always null. Because p ["dh"] and dr ["dh"] are of the object type. However, if you output:
? P ["dh"]. GetType (). Name // output Int32. This is caused by the binning and unboxing of the Value Type ValueType of the Object.
Let's take a look at the following test code:
Object a = 11; object B = 11; Console. WriteLine (a = B); // equivalent to ReferenceEquals. Of course, the comparison references are not equal! Console. writeLine (object. referenceEquals (a, B); // falseConsole. writeLine (object. equals (a, B); // trueConsole. writeLine (. equals (B); // true
Equals is recommended for comparing Datarow!
From: slmk