使用“是对象"是指对象.而不是“!= null"或Object.ReferenceEquals() [英] Using "is object" instead of "!= null" or Object.ReferenceEquals()

查看:46
本文介绍了使用“是对象"是指对象.而不是“!= null"或Object.ReferenceEquals()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一直让我感到困扰的是C#没有专用的引用相等运算符,不能重载该运算符.要测试引用是否为空,我想编写如下代码:

It has always bothered me that C# doesn't have a dedicated reference equality operator, one that can't be overloaded. To test if a reference is null, I want to write code like this:

if (thing == null)

但是总是有这样的想法:如果类重载了==运算符,该怎么办?".我对该类是否认为该对象等效于null并不感兴趣.我对对象 reference 是否为空感兴趣.替代方法似乎是在强制转换为对象:

But there's always this nagging thought, "What if the class has overloaded the == operator?". I'm not interested in whether the class considers the object equivalent to null. I'm interested in whether the object reference is null. The alternatives seem to be casting to object:

if ((object)thing == null)

和Object.ReferenceEquals():

and Object.ReferenceEquals():

if (Object.ReferenceEquals(thing, null)) // long form
if (ReferenceEquals(thing, null)) // short form

但是最近我一直在写这样的代码:

But recently I have been writing code like this:

if (thing is object) // thing != null
if (!(thing is object)) // thing == null

我将其读为如果事物是​​一个对象",也就是说它被设置为一个对象.我意识到这不是"is"运算符的目的,但是它确实检查空引用,并且所有引用类型都从对象继承,所以...为什么不呢?

I read this as "if thing is an object", which is to say that it's set to an object. I realize this isn't the purpose of the "is" operator, but it does check for null references and all reference types inherit from object, so... why not?

我发现,至少对我来说,这样的代码更易读,而且键入起来更舒适,尤其是因为肯定性情况(事物是对象)在我的代码中比否定情况(!(事物)更常见.是对象).

I found that, to me at least, code like this is more readable and much more comfortable to type, especially since the affirmative case (thing is object) is much more common in my code than the negative case (!(thing is object)).

所以我的问题是,是否有我不知道的陷阱或极端情况?它被认为是不好的做法还是效率低下?令人困惑吗?为什么我看不到这样的代码?

So my question is, are there any pitfalls or edge cases that I'm not aware of? Is it considered bad practice or inefficient? Is it confusing? Why don't I ever see code like this?

推荐答案

使用

if (thing is object)
  ...

您混淆了要执行的操作-检查 null .此刻对您而言可能是显而易见的,但可能在几个月后(如果您已放弃该做法)就不会出现了,并且对其他任何人来说显然都不是显而易见的.如果遇到此问题,会让我对作者的意图感到困惑.而且,如果您需要评论以解释自己的工作,请不要这样做.(当然,在某些情况下您可以并且应该这样做,但绝对不是像 null -check这样简单的事情.)

you obfuscate what you want to do - check for null. It might be obvious and clean to you at the moment, but it might not be in a few month (given you've dropped that practice) and it's certainly not obvious for anyone else. If I encountered this, it'd leave me puzzled about the intention of the author. And if you need a comment to explain what you do ... don't do it. (Of course there are situations where you can and should, but definitely not for something as simple as a null-check.)

最终,您将使代码的可维护性降低,因为理解您的代码将始终需要双重考虑.帮自己一个忙,并保持代码尽可能整洁,这意味着对您的意图诚实.

Eventually you will render your code less maintainable, since understanding your code will always need a double take. Do yourself a favour and keep your code as clean as possible and this means being honest about your intentions.

这篇关于使用“是对象"是指对象.而不是“!= null"或Object.ReferenceEquals()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆