Why does Enum.CompareTo () throw a NullReferenceException?

I identified this oddity today in the original source for the .NET Framework, and I wonder if the answer will lead to an interesting regional case in the implementation.

The implementation Enum.CompareTo()includes an 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 be checked for null ?

I would expect the mode itself to take care of this - and really write a lot of code under this assumption.

+4
source share

Source: https://habr.com/ru/post/1655411/


All Articles