Confused object. Equals static method in C #

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.