Calling IEquatable.Equals from an integer 0 to test against a null object should return false but when using reflection it returns true. Given the following code, I would expect for iEquals and equals to return 'false', but the reflection code returns true, why? I am on C# 7.3 and .NET Framework 4.6.2:
int zero = 0;
object nullObj = null;
var iEquals = zero.GetType().GetInterface("IEquatable`1").GetMethod("Equals");
var reflectionEquals = iEquals.Invoke(zero, new[] { nullObj }); // true
var equals = ((IEquatable<int>)zero).Equals(nullObj); // false
From the
MethodBase.Invokedocs:For
intzero-initialized instance is0, henceTrue. Another example can bebool- for booleans zero-initialized instance isfalseso:Demo @sharplab.io