English 中文(简体)
为什么在比较无效时表示反对?
原标题:Why casting to object when comparing to null?
最佳回答

一种类型有可能超载=操作员。 投了反对票,以确保使用原始定义。

问题回答

正如其他人所说的那样,这种类型可能高于=操作员。 因此,加入<代码> 目标等于<代码>if(目标.ReferenceEquals(p, 无){>>。

我认为,我们投下了系统。 反对意见将使你绕过任何操作者可能拥有的双D点。

有可能避免与超载=操作员混淆。 如果该插物不存在,且该 = 操作器被超负荷。 现在p = 无 线路可能与运营商挂钩=。 运营商的许多实施=仅服从压倒性<代码>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;
    }

    ...
}

加入<代码> 反对: 作者确保对<代码>null进行简单的参考检查(即预期的)。

这可能是大样本的一部分,因为=操作员超负荷工作。 在该案中,如果使用斜线 = 无,则可能会导致StackOver流程。 平等(目标)被援引为=定义的一部分。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签