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.