C # object. Equals Method

Source: Internet
Author: User
System. Object objects are directly or indirectly inherited classes of all managed types.
For example, all value types are inherited from system. valuetype; while system. valuetype is inherited from system. object; however, system. valuetype overrides system. semantics related to implementation value types in object.

The system. Object. Equals method has two implementations:
Public Virtual boolEquals(Object OBJ)
Public static boolEquals(Object obja, object objb)
We use the decompilation tool to decompile system. DLL to obtain the source code of these two methods:

Public Virtual boolEquals(Object OBJ) {return internalequals (this, OBJ );}
Public static boolEquals(Object obja, object objb) {If (obja = objb) {return true;} If (obja! = NULL) & (objb! = NULL) {return obja. Equals (objb);} return false ;}

Let's further investigate the interalequals method and get the following implementation:

[Methodimpl (methodimploptions. internalcall)] internal static extern boolInternalequals
(Object obja, object objb );

Here we can see the call sequence and principle of equals in the implementation process.

When equals is implemented for each object, most of the reference types overwrite the relevant semantics. The comparison method of equals in a specific object is as follows,
We can use ildasm disassembly to obtain the Il center.Code.
After I have studied the msil syntax for a while, I will reference some more convincing disassembly intermediate code.

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.