今天在.NET Framework的参考源中发现了这种奇怪的现象,我想知道答案是否会导致实现中一个有趣的边缘情况。
Enum.CompareTo()
的实施包括明确检查this==null
:
public int CompareTo(Object target)
{
// ... elided ...
if (this == null)
throw new NullReferenceException();
Contract.EndContractBlock();
// ... elided ...
}
完整来源:http://referencesource.microsoft.com/mscorlib/system/enum.cs.html#b50f2b9e3118e0d6
为什么此代码需要检查 null 本身?
我原本期望运行时本身可以解决这个问题 - 事实上,在这个假设下编写了很多代码。