测试 null 引用始终返回 false...即使为 null

发布于 2025-01-02 10:27:22 字数 178 浏览 1 评论 0原文

如果我使用 Visual C# 2010 编译以下代码片段,我总是得到错误:

object o = null;
Console.WriteLine("Is null: " + o == null); // returns false

有人知道为什么吗???

If I compile the following code snippet with Visual C# 2010 I ALWAYS get false:

object o = null;
Console.WriteLine("Is null: " + o == null); // returns false

Does anybody know why???

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

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

发布评论

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

评论(3

┼── 2025-01-09 10:27:22

运算符优先级。

尝试

Console.WriteLine("Is null: " + (o == null));

在您的代码中,首先将 o 添加到 "Is null: " 字符串,然后检查它是否为 null。当然不是,所以判断为 false。您的调用与您只写这一样,

Console.WriteLine(false.ToString());

这就是为什么即使没有您的字符串,也只打印“False”的原因。

Operator precedence.

Try

Console.WriteLine("Is null: " + (o == null));

In your code, First o is added to "Is null: " string, which is then checked if it is null. Of course it isn't, so it is evaluated as false. Your call is the same as if you would just write

Console.WriteLine(false.ToString());

This is why only "False" is printed, even without your string.

像极了他 2025-01-09 10:27:22

为什么很容易;想想你所写的实际上是这样的:

object o = null;
Console.WriteLine(("Is null: " + o) == null); // returns false

它正在测试 "Is null: " + onull,它总是 false 。这是由于运算符优先级规则所致,其中 + 位于 == 之前。

您应该显式应用括号以确保它按您想要的方式工作:

Console.WriteLine("Is null: " + (o == null)); // returns true

正如 Jim Rhodes 的评论中所述:

这是您应该始终使用括号并且永远不要依赖编译器优先规则的几个原因之一。

我指出我自己同意;我什至不自己尝试记住运算符优先级规则,而是始终明确地使用括号。我进一步建议,这也是在依赖隐式类型转换和/或具有多个重载的方法时要非常小心的原因之一。

我还想指出,我真的很喜欢Ravadre中提到的Ravadre .com/a/9150029/237838">他们的答案;关于为什么只打印“False”,而不是您尝试打印的整个文本。

Why is easy; think of what you've written as actually being this:

object o = null;
Console.WriteLine(("Is null: " + o) == null); // returns false

It's testing "Is null: " + o against null, which will always be false. This is due to the rules of operator precedence, where + comes before ==.

You should explicitly apply parens to make sure it's working like you want:

Console.WriteLine("Is null: " + (o == null)); // returns true

As noted in the comments by Jim Rhodes:

This is one of several reasons why you should ALWAYS use parentheses and never rely on compiler precedence rules.

I noted myself that I agree; that I don't even try to remember operator precedence rules myself, instead being explicit with parens all the time. I further suggest that this is also one reason to be very careful when relying on implicit type conversion and/or methods with multiple overloads.

I'd also like to point out that I really like something Ravadre noted in their answer; about why only "False" was printed, and not the whole text you were trying to print.

岁月如刀 2025-01-09 10:27:22

其他答案已经正确诊断了问题:连接的运算符优先级高于相等。然而,没有人解决的是您的程序中更根本的错误,即您根本就在进行串联。编写代码的更好方法是:

Console.WriteLine("is null: {0}", obj == null);

现在不可能存在运算符优先级问题,因为相关表达式只有一个运算符。

一般来说,在执行输出时,您应该避免字符串连接,并支持字符串替换。它更容易正确,更灵活,更容易本地化使用此技术编写的程序,等等。

The other answers have correctly diagnosed the problem: operator precedence is higher for concatenation than equality. What no one has addressed however is the more fundamental error in your program, which is that you are doing concatenation at all. A better way to write the code is:

Console.WriteLine("is null: {0}", obj == null);

Now there cannot possibly be an operator precedence problem because the expression in question only has a single operator.

In general you should avoid string concatenation, and favour string substitution, when performing output. It is easier to get it right, it is much more flexible, it is easier to localize programs written using this technique, and so on.

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