String.IsNullOrEmpty(myString) 与 myString != null
这三者哪一个更好呢?
string myString = "";
String.IsNullOrEmpty(myString);
vs
string myString = "";
if(myString.Length > 0 || myString != null)
vs
string myString = "";
if (m.Length > 0 | m != null)
前者更清楚,但它们之间有性能差异吗?如果字符串永远不为空,就像从文本框中取出的那样,它可能为空但不能为 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
好吧,问题中的版本:
肯定会更糟,因为您应该测试
null
first (不是第二) - 理想情况下在null
上短路,这样您就不会尝试调用.Length
。但通常我只会使用 string.IsNullOrEmpty 。如果需要,您始终可以编写一个扩展方法以使其不那么冗长(您可以对null
值调用扩展方法)。Well, the version in the question:
would definitely be worse, as you should test for
null
first (not second) - ideally short-circuiting onnull
so you don't attempt to call.Length
. But generally I'd just usestring.IsNullOrEmpty
. You could always write an extension method to make it less verbose if you want (you can call extension methods onnull
values).使用
string.IsNullOrEmpty(str)
。它更清晰、更简洁。它不会成为您应用程序的瓶颈。如果您只需要检查字符串“空”,那么我会检查
string.Empty
因为它更好地表达了您的意图。Go with
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.我会使用 IsNullOrEmpty。
以后再看代码的时候会更容易解析。
这是另一个有点奇怪的原因。后来的一些程序员肯定会出现,抓着胡子说“我认为 myString.trim().Length != 0 更好”并更改它。
正如其他人指出的那样:检查空秒是一个潜在的空访问错误正在等待发生 - 库例程保证没问题。
I'd use the 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.
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.
正如其他人所说,出于可维护性的目的,IsNullOrEmpty() 优于手动检查,并且由于 JIT 编译器关于内联的运行时决策,性能不太可能受到影响(请参阅 Eric Gunnerson 的评论)。
如果其他人想知道实际的 .NET 实现是什么样子,请参阅以下 .NET 4 代码:
该属性指示该方法也将内联到 NGen(即本机)图像中。
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).
In case anyone else is wondering what the actual .NET implementation looks like, here is the .NET 4 code:
That attribute indicates the method will also be inlined in NGen (that is, native) images.
如果您对如何测试字符串引用的不同状态不放心(显然您就是这样,因为您弄错了……;),那么 String.IsNullOrEmpty 是更好的选择。
使用
IsNullOrEmpty
方法:相当于对 null 和零长度使用短路测试:
如果您知道引用不能为 null,则可以跳过该检查并仅检查长度
:
IsNullOrEmpty
方法也适用于正常情况,但在出现问题且引用实际上为 null 的情况下,IsNullOrEmpty
方法会默默地接受它,而您通常会这样做希望被告知该错误。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... ;).Using the
IsNullOrEmpty
method:is equivalent to using a short circuit test for null and zero length:
If you know that the referene can't be null, you can skip that check and just check the length:
The
IsNullOrEmpty
method would also work for normal situations, but in the case where something went wrong and the reference is actually null, theIsNullOrEmpty
method would silently accept it, while you would normally want to be made aware of the error.实现为:
if (s == null || s.Length == 0) ... 。
我相信 String.IsNullOrEmpty(String s)在 API 中
I believe the String.IsNullOrEmpty(String s) is implemented as:
if (s == null || s.Length == 0) ...
in the API.
这是错误的。尝试一下,您将得到一个异常,因为将尝试这两个语句。如果 s 为 null,则 s.Length 将抛出异常。
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.