locked
Implementation of bool object.Equals(object obj) RRS feed

  • Question

  • Can anyone tell me where in the source code of the .NET runtime I can find the actual implementation of the Equals method of the Object class? Everywhere I have looked shows it calling into some function that I can't actually find the code for (one version of Object.Equals I found calls bool RuntimeHelpers.Equals(object o1, object o2) but then I was unable to find the code that actually implements that function)

    Sunday, June 28, 2020 3:09 PM

Answers

All replies

  • Hi jonwil,

    Thank you for posting here.

    For the equals method in RuntimeHelpers.Equals(object o1, object o2):

            [System.Security.SecuritySafeCritical]  // auto-generated
            [ResourceExposure(ResourceScope.None)]
            [MethodImplAttribute(MethodImplOptions.InternalCall)]
            public new static extern bool Equals(Object o1, Object o2);

    The method is part of the type and is declared with the extern C# keyword to indicate that the method is implemented in some external component implemented in native code(C++), and with the value MethodImplOptions.InternalCall, the method is implemented within the CLR, which is part of the implementation of the Virtual Execution System.

    Reference you may need:

    Object Equals - whats the basic logic for pure objects or reference types that don't override Equals?

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, June 29, 2020 2:14 AM
  • For .NET 5, see ecalllist.h and objectnative.cpp. The "loader trick to replace Equals with EqualsValue for value types" seems to have been disabled in classcompat.cpp and methodtablebuilder.cpp.
    • Marked as answer by jonwil Tuesday, June 30, 2020 3:51 AM
    Monday, June 29, 2020 2:21 PM
  • Thanks, that's exactly what I wanted (and couldn't find myself).

    Tuesday, June 30, 2020 3:53 AM