The main confusion here is that you're assuming that all .NET libraries (in this case, the Extended Numerics Library, which is not a part of the BCL) are written in standard C#. This isn't always the case, and different languages have different rules.
In standard C#, the piece of code you're seeing would result in a stack overflow, due to the way operator overload resolution works. However, the code isn't actually in standard C# - it basically uses undocumented features of the C# compiler. Instead of calling the operator, it emits this code:
ldarg.0
ldarg.1
ceq
ret
That's it :) There is no 100% equivalent C# code - this simply isn't possible in C# with your own type.
Even then, the actual operator isn't used when compiling C# code - the compiler does a bunch of optimizations, like in this case, where it replaces the op_Equality
call with just the simple ceq
. Again, you can't replicate this in your own DoubleEx
struct - it's compiler magic.
This certainly isn't a unique situation in .NET - there's plenty of code that isn't valid, standard C#. The reasons are usually (a) compiler hacks and (b) a different language, with the odd (c) runtime hacks (I'm looking at you, Nullable
!).
Since the Roslyn C# compiler is oepn source, I can actually point you at the place where overload resolution is decided:
The place where all binary operators are resolved
The "shortcuts" for intrinsic operators
When you look at the shortcuts, you'll see that equality between double and double results in the intrinsic double operator, never in the actual ==
operator defined on the type. The .NET type system has to pretend that Double
is a type like any other, but C# doesn't - double
is a primitive in C#.