Spotted this oddity today in the reference source for the .NET Framework and I'm wondering if the answer will lead to an interesting edge case in the implementation.
The implementation of Enum.CompareTo()
includes the explicit check this==null
:
public int CompareTo(Object target)
{
// ... elided ...
if (this == null)
throw new NullReferenceException();
Contract.EndContractBlock();
// ... elided ...
}
Full source: http://referencesource.microsoft.com/mscorlib/system/enum.cs.html#b50f2b9e3118e0d6
Why does this code need to check for null itself?
I'd have expected the runtime itself to take care of this - and indeed have written a lot of code under that assumption.