Because it seems that the compiler is optimizing this. I tried this code:
System.Drawing.Point point = new System.Drawing.Point(0,0); Console.WriteLine((point == null));
And he generated the following IL:
IL_0000: ldloca.s 00 IL_0002: ldc.i4.0 IL_0003: ldc.i4.0 IL_0004: call System.Drawing.Point..ctor IL_0009: ldc.i4.0 IL_000A: call System.Console.WriteLine
It ultimately boils down to "Create a point and then write false to the command line"
It also explains why it does not call your operator. The structure can never be null, and in cases where the compiler can guarantee that you will always get false as a result, it does not bother issuing code to call the operator at all.
The same thing happens with this code, although String is a class and overloads the == operator:
System.Drawing.Point point = new System.Drawing.Point(0,0); Console.WriteLine("foo" == null);
Regarding immutability ... The == operator in C # is usually interpreted as "reference equality", for example. these two variables point to the same instance of the class. If you overload it, then you usually say that two instances of a class, and not the same instance, should behave as if they were the same instance when their data is the same. A classic example is Strings. "A" == GiveMeAnA() , although the actual String reference returned by GiveMeAnA may not be the same as that represented by the literal "A" .
If you overloaded the == operator to classes that were not immutable, then the class mutation after == been evaluated, can cause many subtle errors.
source share