为什么在与 null 比较时强制转换为对象?

发布于 2024-09-12 16:12:37 字数 676 浏览 8 评论 0原文

在浏览有关 Equals 覆盖的 MSDN 文档时,有一点引起了我的注意。

此特定页面的示例中,一些进行 null 检查,并且在进行比较时将对象强制转换为 System.Object 类型:

public override bool Equals(System.Object obj)
{
    // If parameter is null return false.
    if (obj == null)
    {
        return false;
    }

    // If parameter cannot be cast to Point return false.
    TwoDPoint p = obj as TwoDPoint;
    if ((System.Object)p == null)
    {
        return false;
    }

    // Return true if the fields match:
    return (x == p.x) && (y == p.y);
}

是否有使用此强制转换的特定原因,或者只是在本示例中忘记了一些“无用”代码?

While browsing the MSDN documentations on Equals overrides, one point grabbed my attention.

On the examples of this specific page, some null checks are made, and the objects are casted to the System.Object type when doing the comparison :

public override bool Equals(System.Object obj)
{
    // If parameter is null return false.
    if (obj == null)
    {
        return false;
    }

    // If parameter cannot be cast to Point return false.
    TwoDPoint p = obj as TwoDPoint;
    if ((System.Object)p == null)
    {
        return false;
    }

    // Return true if the fields match:
    return (x == p.x) && (y == p.y);
}

Is there a specific reason to use this cast, or is it just some "useless" code forgotten in this example ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

风和你 2024-09-19 16:12:38

我相信转换为 System.Object 可以让您绕过 TwoDPoint 可能具有的任何运算符重载。

I believe casting to System.Object would get you around any operator overloading that TwoDPoint might have.

幽梦紫曦~ 2024-09-19 16:12:38

它的存在可能是为了避免与重载的 == 运算符混淆。想象一下,如果强制转换不存在并且 == 运算符被重载。现在,p == null 行可能会绑定到运算符 ==。许多运算符 == 的实现只是遵循重写的 Equals 方法。这很容易导致堆栈溢出情况

public static bool operator==(TwoDPoint left, TwoDPoint right) {
  return left.Equals(right);
}

public override bool Equals(System.Object obj) {
    ...
    TwoDPoint p = obj as TwoDPoint;
    if ( p == null ) {  // Stack overflow!!!
        return false;
    }

    ...
}

通过转换为 Object,作者确保对 null 进行简单的引用检查(这正是预期的目的)。

It likely exists to avoid confusion with an overloaded == operator. Imagine if the cast did not exist and the == operator was overloaded. Now the p == null line would potentially bind to the operator ==. Many implementations of operator == simply defer to the overridden Equals method. This could easily cause a stack overflow situation

public static bool operator==(TwoDPoint left, TwoDPoint right) {
  return left.Equals(right);
}

public override bool Equals(System.Object obj) {
    ...
    TwoDPoint p = obj as TwoDPoint;
    if ( p == null ) {  // Stack overflow!!!
        return false;
    }

    ...
}

By casting to Object the author ensures a simple reference check for null will occur (which is what is intended).

卖梦商人 2024-09-19 16:12:38

这可能是 == 运算符重载的较大示例的一部分。在这种情况下,如果将 TwoDPoint.Equals(object) 作为 == 定义的一部分调用,则使用 obj == null 可能会导致 StackOverflow。

This might have been part of a larger sample where the == operator was overloaded. In that case, using obj == null could have resulted in StackOverflow if TwoDPoint.Equals(object) was invoked as part of the == definition.

り繁华旳梦境 2024-09-19 16:12:37

类型可能会重载 == 运算符。转换为对象可确保使用原始定义。

It is possible for a type to overload the == operator. The cast to object ensures that the original definition is used.

日裸衫吸 2024-09-19 16:12:37

正如其他人所说,类型可能会覆盖 == 运算符。因此,转换为 Object 相当于 if (Object.ReferenceEquals(p, null)) { ... }

As others said, the type might override the == operator. Therefore, casting to Objectis equivalent to if (Object.ReferenceEquals(p, null)) { ... }.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文