I have two objects(of the same type) which contains a prop myprop of type byte?. The properties are set to null. When I perform objectA.myprop.Equals(objectB.myprop) I get ‘true’ as a result although the MSDN code sample states that “Equals applied to any null object returns false.”
I’m guessing C# uses a seperate overload for nullable type comparisons. I would like to know how C# internally treats objects versus nullable types in this case.
When you call it like that, it will use
Nullable<T>.Equals(object)which shows the expected behaviour in the documentation:(Return value is
trueif…)Likewise for equality via
==, section 7.3.7 of the C# 4 spec (lifted operators) states:(Emphasis mine.)
This is a general rule, in terms of the implementation of
object.Equals:So while it is a general rule, it doesn’t apply in this specific case – because the value here isn’t an object reference – it’s a value type value. It’s still somewhat surprising, unless you basically accept that
Nullable<T>is a bit of a special case – it has specific C# compiler support and it has CLR support in terms of boxing and unboxing.