The original article was published on 23:06:34 in my Netease blog, and was deleted by me (mainly to consider the technology-related Article )
I have been reading <pro C #.. Net 3.0, Special Edition>, which is described in Chapter 3.18.5. the equals and referenceequals methods of static object members. equals (object1, object2) is used to compare whether two objects of the same type are in the same State. the following is an example:
Person person3 = new person ("Fred", "Jones", "222-22-2222", 98 );
// The person class has four attributes: fname (string), lname (string), SSN (string), age (byte)
Person person4 = new person ("Fred", "Jones", "222-22-2222", 98 );
According to this statement, person3 and person4 have the same status. Therefore, object. the return value of equals (person3, person4) should be true. However, after compiling in Visual Studio 2005, the result is: false. it was really surprising. later, I checked the object on the Internet. the equals method is written as follows:
Decompile system. DLL using the decompilation tool to obtain the implementation source code:
Public static bool equals (Object obja, object objb)
{
If (obja = objb)
{Return true ;}
If (obja! = NULL) & (objb! = NULL ))
{Return obja. Equals (objb );}
Return false;
}
We can see that whether two objects are equals depends on whether the values determined by the equal sign are equal. the equal sign determines whether two objects are equal. It determines whether the two objects have the same reference (whether it points to the location of the same object in memory ). although the two objects are in the same State, they do not point to the same region in the memory and do not belong to the same reference. therefore, the returned value is false.