简体   繁体   English

为什么在将值与常量进行比较之前检查null?

[英]Why checking for null before comparing a value to a constant?

Sometimes, a value must be checked for equality with a constant. 有时,必须使用常量检查值是否相等。 In such cases, I've always seen the code like this: 在这种情况下,我总是看到这样的代码:

if (!string.IsNullOrEmpty(text))
{
    if (text == "Some text here")¹
    {
        // Do something here.
    }
}

In my case, I would rather write: 就我而言,我宁愿写:

if ("Some text here".Equals(text))
{
    // Do something here.
}

After all, if text is null , Equals will return false , which is expected. 毕竟,如果textnull ,则Equals将返回false ,这是预期的。 The inversion of a constant and a variable feels strange, but is still understandable for a beginner, and avoids the NullReferenceException which would be thrown with text.Equals("Some text here") . 常量和变量的反转感觉很奇怪,但对初学者来说仍然是可以理解的,并且避免了使用text.Equals("Some text here")抛出的NullReferenceException

Am I missing something? 我错过了什么吗?

Why all source code I've seen use the syntax from the first example, and never from the second one? 为什么我见过的所有源代码都使用第一个示例中的语法,而不是第二个示例中的语法?


¹ In real code, it would rather be a constant or a readonly field. ¹在实际代码中,它宁愿是常量字段或只读字段。 To shorten the examples, I put the strings inline. 为了缩短示例,我将字符串内联。

In such cases, I've always seen the code like this: 在这种情况下,我总是看到这样的代码:

You're right to think that's odd and unnecessary, because it is. 你认为这是奇怪的和不必要的是正确的,因为它是。 It's a completely superfluous null or empty check. 这是一个完全多余的null或空检查。 Frankly, I'd admonish code like that in a code review. 坦率地说,我会在代码审查中告诫这样的代码。

if (text == "Some text here") {
    // Do something here.
}

is perfectly fine and that's what I'd use. 非常好,这就是我使用的。

Am I missing something? 我错过了什么吗?

No, you're not missing anything. 不,你没有遗漏任何东西。

Why all source code I've seen use the syntax from the first example, and never from the second one? 为什么我见过的所有源代码都使用第一个示例中的语法,而不是第二个示例中的语法?

Because you're looking for love in all the wrong places? 因为你在所有错误的地方寻找爱情?

There is nothing wrong with just this. 这没有什么不妥。

if (text == "Some text here")
{
    // Do something here.
}

There is no need to check for null/empty, since it won't be equal anyway. 无需检查null / empty,因为它无论如何都不会相等。

If you wish to use the Equals method, there is a version that isn't sensitive to null values. 如果您希望使用Equals方法,则有一个对空值不敏感的版本。

if (string.Equals(text, "Some text here"))
{
    // Do something here.
}

Your version is fine so long as you have a literal or something that you know will not be null . 只要你有一个文字或你知道不会为null东西,你的版本就可以了。

If, on the other hand, you had 另一方面,如果你有

if (text1.Equals(text2))

then clearly you would need to defend against text1 being null . 那么显然你需要防御text1null

But I would stop using Equals here and use == which removes the need for the null check 但是我会在这里停止使用Equals并使用==这样就无需进行null检查

if (text1 == text2)

Are you preferring to use Equals because of your Java roots? 您是否因为Java根源而更喜欢使用Equals

I would think it is in human nature, when you compare smth, you always compare "your" stuff against someone else. 我认为这是人性,当你比较smth时,你总是将“你的”东西与别人比较。 not the other way around, i think same natual thinking went to C# coding as well :) 不是相反,我认为同样的自然思想也去了C#编码:)

I would just use if (text == "Some text here") . 我会使用if (text == "Some text here") It's clear, concise, and fast. 它清晰,简洁,快速。 The IsNullOrEmpty check you refer to might be a (most likely useless) micro-optimization. 您引用的IsNullOrEmpty检查可能是(最可能无用的)微优化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM