方法 returns null,为什么检查 null 会转到泛型重载?

The method returns null, why does checking for null go to generic overload?

    while (currentNode?.BinaryComp(_value) != null);


    public static bool operator !=(Node<T> f1, Node<T> f2)
    {
        return f1.Value.CompareTo(f2.Value) != 0;
    }

System.NullReferenceException:

您可以使用 ReferenceEquals。此外,如果 f1 或 f2 为 null,则 f1.Value 将抛出异常。使用 f1?.Value 或使用 referenceequals.

您的代码应如下所示:

public static bool operator !=(Node<T> f1, Node<T> f2)
{
    if (object.ReferenceEquals(f1, null))
    {
         return object.ReferenceEquals(f2, null);
    }

    return f1.Value.CompareTo(f2.Value);
}

这是一篇关于 Equals、==、ReferenceEquals 的有趣文章

C# .Equals(), .ReferenceEquals() and == operator