0

I wanted to look at the implementation of System.Type.Equals, and while I was browsing the file in VS I found that it also contains a == overload. Of course I want to make sure it does what I think it does, so I head over to the C# official documentation. They don't have it there, so then my next instinct is to look at the C# source, I find the specific line, but then it says that the == operator is external, which makes me think that there's a DLL or some C++ file I could look at, but even after I did a file search I could not find any C++ or DLL file.

I want to make sure that System.Type.Equals and System.Type.== are equivalent.

Where should I go to view a more specific implementation of this function? And how should I search for things like these moving forward?

Jaacko Torus
  • 796
  • 7
  • 24
  • .net core source is on github - all of it https://github.com/dotnet/core – pm100 Feb 22 '22 at 22:16
  • Out of curiosity, is there some reason you are looking into the implementations of `System.Type.Equals` vs `System.Type.==`? (Curiosity is a fine answer, just want to know if you have some strange behaviour you are trying to explain or not.) – Andrew McClement Feb 22 '22 at 22:27
  • 1
    @AndrewMcClement No, I just want to learn, how each they work, specially because `System.Type.==` is not in the documentation. – Jaacko Torus Feb 22 '22 at 22:29
  • 1
    C# official docs for `==`: https://learn.microsoft.com/en-us/dotnet/api/system.type.op_equality?view=net-6.0 For `Equals`: https://learn.microsoft.com/en-us/dotnet/api/system.type.equals?view=net-6.0 – Andrew McClement Feb 22 '22 at 22:31
  • 1
    @pm100: that's not the right repo for the basics; that would be https://github.com/dotnet/runtime. That's a little hard to spit through, though -- you can find `Type` [here](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Type.cs#L560). Note that Core uses a different approach by marking it `[Intrinsic]` rather than using the `InternalCall` business -- on Framework, it always defers to a C++ implementation, on Core, there is still a managed implementation and we just hint the JIT that it may elide things. – Jeroen Mostert Feb 22 '22 at 22:31
  • @JeroenMostert would lines 557-570 of https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Type.cs be the relevant code? – Andrew McClement Feb 22 '22 at 22:34
  • @AndrewMcClement: yes, my link goes straight to `operator ==`. – Jeroen Mostert Feb 22 '22 at 22:35
  • Ah, right, I think I missed the 2nd link, sorry. – Andrew McClement Feb 22 '22 at 22:36
  • The actual implementation of the JIT intrinsic then in turn depends on the runtime, but the Core implementation is [here](https://github.com/dotnet/coreclr/blob/master/src/jit/importer.cpp#L3947) (`CORINFO_INTRINSIC_TypeEQ`). For Framework, I believe the online reference source actually doesn't have any of the C++ stuff and you may have to download the old Rotor package, but it's been a long time since I looked up internal calls there so I'm not sure. – Jeroen Mostert Feb 22 '22 at 22:37
  • 1
    To make a very long story short, though, the Core implementation of `operator==` actually helpfully documents the instances where this would *not* give the same result as `Type.Equals` ("If `left` is a non-runtime type with a weird Equals implementation this is where operator `==` would differ from `Equals` call") but you're unlikely to encounter them in practice. – Jeroen Mostert Feb 22 '22 at 22:39
  • Thank you all for the clear responses. And thank you for the resources, now I know where to find all this info moving forward. pm100 JeroenMostert AndrewMcClement – Jaacko Torus Feb 22 '22 at 22:49

0 Answers0