C# string.IsNullOrEmpty() 与 string.NotNullOrEmpty()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/734372/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
string.IsNullOrEmpty() vs string.NotNullOrEmpty()
提问by Chris S
I'm curious if any developers use string.IsNullOrEmpty() more often with a negative than with a positive
我很好奇是否有任何开发人员使用 string.IsNullOrEmpty() 更频繁地使用负数而不是正数
e.g.
例如
if (!string.IsNullOrEmpty())
This is how I use the method 99% of the time. What was the design decision for this?
这就是我 99% 的时间使用该方法的方式。对此的设计决策是什么?
采纳答案by Kent Boogaart
Because "IsNullOrEmpty" is easier to understand than "NotNullOrEmpty". The latter could be interpreted as:
因为“IsNullOrEmpty”比“NotNullOrEmpty”更容易理解。后者可以解释为:
- It's not null and it's not empty
- It's not null or it is empty
- 它不是空的,也不是空的
- 它不为空或为空
回答by John Kraft
That is the most common usage I have seen.
这是我见过的最常见的用法。
回答by Erich Mirabal
C# naming conventions dictate that your expressions should be in the positive such as "Is..." and not "IsNot..."
C# 命名约定规定您的表达式应该是正数,例如“Is...”而不是“IsNot...”
EDIT: Typically, I use it when doing error checking and input validation at the beginning of a method and raise an exception if the parameter is null or empty.
编辑:通常,我在方法开始时进行错误检查和输入验证时使用它,如果参数为 null 或为空,则会引发异常。
if (string.IsNullOrEmpty(myParameter))
{
throw new ....
}
if (string.IsNullOrEmpty(myParameter))
{
throw new ....
}
回答by Mehrdad Afshari
Double negatives are usually discouraged in naming stuff. !string.NotNullOrEmpty(...)
would make one.
在命名东西时通常不鼓励双重否定。!string.NotNullOrEmpty(...)
会做一个。
回答by uriDium
Personally I prefer to cater for the non negated scenario first. It just makes sense to me to do the true part first and then the false. Comes down to personal style.
就我个人而言,我更喜欢首先满足非否定的情况。对我来说,先做真实的部分,然后做虚假的部分才有意义。归结为个人风格。
回答by Guffa
Perhaps because then the name would have to be the lengthy IsNotNullAndNotEmpty
to be as specific.
也许是因为那样名称必须很长IsNotNullAndNotEmpty
才能如此具体。
回答by Brian Genisio
I prefer the extension method:
我更喜欢扩展方法:
public static class StringExtensions
{
public static bool IsNullOrEmpty(this string value)
{
return string.IsNullOrEmpty(value);
}
}
I find it reads better to say:
我觉得这样说更好看:
if(myValue.IsNullOrEmpty())
or
或者
if(!myValue.IsNullOrEmpty())
回答by stevehipwell
I've always thought it seemed the wrong way round as I use the negative much more often than the positive.
我一直认为这似乎是错误的方式,因为我使用否定词的频率比肯定词要多得多。
I would also like there to be an instance IsEmpty() or IsNotEmpty() for use when the variable is declared within the function. This could not be IsNullOrEmpty() or IsNotNullOrEmpty() as if the instance was null then you would get a null reference exception.
我还希望有一个实例 IsEmpty() 或 IsNotEmpty() 用于在函数中声明变量时使用。这不能是 IsNullOrEmpty() 或 IsNotNullOrEmpty(),就像实例为空一样,那么您将获得空引用异常。
回答by tvanfosson
For those logicians out there, !string.IsNullOrEmpty is not equivalent to string.IsNotNullOrEmpty. @Guffa has it correct. Using DeMorgan's law, it would have to be string.IsNotNullAndNotEmpty to be equivalent.
对于那些逻辑学家来说,!string.IsNullOrEmpty 不等同于 string.IsNotNullOrEmpty。@Guffa 说得对。使用德摩根定律,它必须是 string.IsNotNullAndNotEmpty 才能等效。
¬(null ∨ empty) ⇔ ¬null ∧ ¬empty
¬(null ∨ 空) ⇔ ¬null ∧ ¬空
¬(null ∨ empty) ≠ ¬null ∨ empty
¬(null ∨ 空) ≠ ¬null ∨ 空
The point here, I guess, is that the way it is currently is unambiguous, where as making the opposite unambiguous would be cumbersome.
我想这里的重点是它目前的方式是明确的,而让相反的方式明确会很麻烦。
回答by Wedge
"NotNullOrEmpty" is ambiguous, it could mean "(not null) or empty" or it could mean "not (null or empty)". To make it unambiguous you'd have to use "NotNullAndNotEmpty", which is a mouthfull.
“NotNullOrEmpty”是模棱两可的,它可能意味着“(非空)或空”,也可能意味着“非(空或空)”。为了使它明确,你必须使用“NotNullAndNotEmpty”,这是一个满口的。
Also, the "IsNullOrEmpty" naming encourages use as a guard clause, which I think is useful. E.g.:
此外,“IsNullOrEmpty”命名鼓励用作保护条款,我认为这很有用。例如:
if (String.IsNullOrEmpty(someString))
{
// error handling
return;
}
// do stuff
which I think is generally cleaner than:
我认为这通常比以下内容更清洁:
if (!String.IsNullOrEmpty(someString))
{
// do stuff
}
else
{
// error handling
return;
}