简体   繁体   English

String.IsNullOrEmpty(myString)vs myString!= null

[英]String.IsNullOrEmpty(myString) Vs myString != null

Which one is better among these three? 哪三个在这三个中更好?

string myString = ""; 
String.IsNullOrEmpty(myString);

vs

string myString = "";
if(myString.Length > 0 || myString != null)

vs 

string myString = "";
if (m.Length > 0 | m != null)

Former is clearer but is there any performance difference among these? 前者更清楚,但这些之间有任何性能差异吗? What if, in case a string is never empty, like if taken from a Text Box, which could be empty but not null ? 如果字符串从不为空,如果从文本框中取出,可能是空的但不是空的,该怎么办?

Well, the version in the question: 好吧,问题中的版本:

if(myString.Length > 0 || myString != null)

would definitely be worse, as you should test for null first (not second) - ideally short-circuiting on null so you don't attempt to call .Length . 肯定会更糟,因为你应该首先测试null (而不是秒) - 理想情况下在null短路,这样你就不会尝试调用.Length But generally I'd just use string.IsNullOrEmpty . 但通常我只使用string.IsNullOrEmpty You could always write an extension method to make it less verbose if you want (you can call extension methods on null values). 如果需要,您可以随时编写扩展方法以使其更简洁(您可以在null值上调用扩展方法)。

static bool HasValue(this string s) {
    return !string.IsNullOrEmpty(s);
}

Go with string.IsNullOrEmpty(str) . 使用string.IsNullOrEmpty(str) It's clearer and more succinct. 它更清晰,更简洁。 It will not be a bottle-neck in your application. 它不会成为您应用的瓶颈。

If you only need to check for string "emptiness", then I would go with a check against string.Empty since it expresses your intent better. 如果你只需要检查字符串“空”,那么我会对string.Empty进行检查,因为它更能表达你的意图。

I'd use the IsNullOrEmpty. 我会使用IsNullOrEmpty。

It will be easier to parse when you are looking through the code later on. 稍后查看代码时,将更容易解析。

Here's another - slightly bizarre - reason. 这是另一个 - 有点奇怪 - 的原因。 Some later programmer is bound to come along later, scratch his beard and say "I think that myString.trim().Length != 0 is better" and change it. 一些后来的程序员后来必定会来,抓胡子说“我认为myString.trim()。长度!= 0更好”并改变它。

As others have pointed out: checking for null second is a potential null access error waiting to happen - the library routine is guaranteed to be ok. 正如其他人所指出的那样:检查null秒是一个潜在的空访问错误等待发生 - 库例程保证是正常的。

As others have said, IsNullOrEmpty() is superior to the manual checks for purposes of maintainability and isn't likely to suffer in performance thanks to the JIT compiler's runtime decisions about inlining (see Eric Gunnerson's comments ). 正如其他人所说的那样,IsNullOrEmpty()在可维护性方面优于手动检查,并且由于JIT编译器关于内联的运行时决策而不太可能在性能上受到影响(参见Eric Gunnerson的评论 )。

In case anyone else is wondering what the actual .NET implementation looks like, here is the .NET 4 code: 如果其他人想知道实际的.NET实现是什么样的,这里是.NET 4代码:

[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}

That attribute indicates the method will also be inlined in NGen (that is, native) images. 该属性表明该方法也将在NGen(即本机)图像中内联。

The String.IsNullOrEmpty is the better choise if you are unsecure about how to test the different states of the string reference (which you obviously are, as you got it wrong... ;). String.IsNullOrEmpty是更好的选择,如果你不安全如何测试字符串引用的不同状态(你显然是,因为你错了......;)。

Using the IsNullOrEmpty method: 使用IsNullOrEmpty方法:

if (String.IsNullOrEmpty(s)) ...

is equivalent to using a short circuit test for null and zero length: 相当于使用零和零长度的短路测试:

if (s == null || s.Length == 0) ...

If you know that the referene can't be null, you can skip that check and just check the length: 如果您知道referene不能为null,则可以跳过该检查并只检查长度:

if (s.Length == 0) ...

The IsNullOrEmpty method would also work for normal situations, but in the case where something went wrong and the reference is actually null, the IsNullOrEmpty method would silently accept it, while you would normally want to be made aware of the error. IsNullOrEmpty方法也适用于正常情况,但是在出现错误并且引用实际为null的情况下, IsNullOrEmpty方法将默默地接受它,而您通常希望知道错误。

I believe the String.IsNullOrEmpty(String s) is implemented as: 我相信String.IsNullOrEmpty(String s)实现为:

if (s == null || s.Length == 0) ... if(s == null || s.Length == 0)...

in the API. 在API中。

I believe the String.IsNullOrEmpty(String s) is implemented as: if (s == null || s.Length == 0) ... in the API.

That's wrong. 那是错的。 Try it and you will get an exception as the two statement will be tried. 尝试它,你将得到一个例外,因为将尝试两个语句。 If s is null, then s.Length will throw an execption. 如果s为null,则s.Length将抛出一个execption。

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

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