String.IsNullOrEmpty(myString) 与 myString != null

发布于 2024-08-19 04:02:30 字数 305 浏览 3 评论 0原文

这三者哪一个更好呢?

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 技术交流群。

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

发布评论

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

评论(7

蛮可爱 2024-08-26 04:02:30

好吧,问题中的版本:

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

肯定会更糟,因为您应该测试 null first (不是第二) - 理想情况下在 null 上短路,这样您就不会尝试调用 .Length。但通常我只会使用 string.IsNullOrEmpty 。如果需要,您始终可以编写一个扩展方法以使其不那么冗长(您可以对 null 值调用扩展方法)。

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

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. But generally I'd just use 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).

static bool HasValue(this string s) {
    return !string.IsNullOrEmpty(s);
}
可是我不能没有你 2024-08-26 04:02:30

使用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.

浅紫色的梦幻 2024-08-26 04:02:30

我会使用 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.

明天过后 2024-08-26 04:02:30

正如其他人所说,出于可维护性的目的,IsNullOrEmpty() 优于手动检查,并且由于 JIT 编译器关于内联的运行时决策,性能不太可能受到影响(请参阅 Eric Gunnerson 的评论)。

如果其他人想知道实际的 .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;
}

该属性指示该方法也将内联到 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:

[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.

☆獨立☆ 2024-08-26 04:02:30

如果您对如何测试字符串引用的不同状态不放心(显然您就是这样,因为您弄错了……;),那么 String.IsNullOrEmpty 是更好的选择。

使用 IsNullOrEmpty 方法:

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

相当于对 null 和零长度使用短路测试:

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

如果您知道引用不能为 null,则可以跳过该检查并仅检查长度

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

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:

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:

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.

傲影 2024-08-26 04:02:30

实现为:

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.

放我走吧 2024-08-26 04:02:30

我相信 String.IsNullOrEmpty(String s) 的实现如下:
if (s == null || s.Length == 0) ...
在 API 中。

这是错误的。尝试一下,您将得到一个异常,因为将尝试这两个语句。如果 s 为 null,则 s.Length 将抛出异常。

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.

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