为什么 is 关键字需要非空表达式?

发布于 2024-07-23 20:49:39 字数 160 浏览 9 评论 0原文

is 关键字的 MSDN 文档说:

expression is not null

为什么? 如果 MethodThatReturnsNull() 类型 被调用,那么不应该返回 false,因为 null 肯定不是该类型?

The MSDN documentation for the is keyword says:

expression is not null

Why? If MethodThatReturnsNull() is type were called shouldn't that return false since null certainly isn't that type?

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

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

发布评论

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

评论(4

记忆で 2024-07-30 20:49:39

如果表达式null,它会返回false。 也许您误解了文档?

It does return false if expression is null. Perhaps you're misunderstanding the documentation?

完美的未来在梦里 2024-07-30 20:49:39

关于 null ,您唯一可以肯定的是您不知道它是什么。 与 null 进行比较的结果通常为 null ...

问: 1 == “我不知道”吗?
答:“我不知道”

查看 这篇博文,作者:Eric Lippert。

The only thing you can say for certain about null is that you don't know what it is. Comparing something to null generally has a result of null ...

Q: Does 1 == "I don't know"?
A: "I dont know"

Check out this blog post by Eric Lippert.

囚你心 2024-07-30 20:49:39

您无法静态解析 null。

You can't statically resolve a null.

不离久伴 2024-07-30 20:49:39

这是我对类似问题给出的一个非常受欢迎的答案。

C# 获取 null 对象的类型

这就像问什么样的蛋糕
会放在一个空盒子里
没有标签。

This was a pretty popular answer I gave to a similar question.

C# get type of null object

That's like asking what kind of cake
would have been in an empty box with
no label.

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