为什么 XName 相等运算符似乎在引用自身?

Why does the XName equality operator appear to refer to itself?

我想知道如何为 XName 定义相等性,并注意到相等性运算符似乎引用自身(使用 ILSpy 2.4.0.1963 查看反编译的 C# 时)

public static bool operator ==(XName left, XName right)
{
    return left == right;
}

这是反编译器中的错误。

它实际上是将两个操作数转换为 object 以通过引用进行比较。

参见actual source

    // The overloads of == and != are included to enable comparisons between
    // XName and string (e.g. element.Name == "foo"). C#'s predefined reference
    // equality operators require one operand to be convertible to the type of
    // the other through reference conversions only and do not consider the
    // implicit conversion from string to XName.

    /// <summary>
    /// Returns a value indicating whether two instances of <see cref="XName"/> are equal.
    /// </summary>
    /// <param name="left">The first XName to compare.</param>
    /// <param name="right">The second XName to compare.</param>
    /// <returns>true if left and right are equal; otherwise false.</returns>
    /// <remarks>
    /// This overload is included to enable the comparison between
    /// an instance of XName and string.
    /// </remarks>
    public static bool operator ==(XName left, XName right) {
        return (object)left == (object)right;
    }