为什么XName相等运算符似乎是指它自己

本文关键字:似乎是 它自己 运算符 XName 为什么 | 更新日期: 2023-09-27 18:00:09

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

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

为什么XName相等运算符似乎是指它自己

这是反编译器中的一个错误。

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

请参阅实际来源:

    // 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;
    }
相关文章: