简体   繁体   中英

String.Empty, null, Length, or String.IsEmptyOrNull?

Which way is really the fastest way to check for an empty string and there is any specific case where need to any specific.

1. String.IsNullOrEmpty()

2. str == null

3. str == null || str == String.Empty

4. str == null || str == ""

5. str == null || str.length == 0

Use option 1.

If you specifically want to check for null or empty strings, then there's no reason to use anything other than string.IsNullOrEmpty . It's the canonical way of doing so in .NET, and any differences in performance will be almost certainly be negligible.

This is a textbook example of premature optimization ; by all means, write efficient code, but don't waste development time over it for no justifiable gain in performance. Remember that your time as a developer is generally far more valuable than the CPU's time.

Quoth Donald Knuth:

We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.

If this level of micro-optimisation is genuinely necessary for your application, then you probably shouldn't be using .NET.

Do you care about whitespace as well?

If whitespace is valid, use String.IsNullOrEmpty , otherwise use String.IsNullOrWhiteSpace (in .Net 4.0 or higher). The latter is equivalent to, but more performant than, String.IsNullOrEmpty(value) || value.Trim().Length == 0; String.IsNullOrEmpty(value) || value.Trim().Length == 0;

see http://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace.aspx

在这个网站上找到了一些关于不同方法的统计数据: http//www.dotnetperls.com/empty-string

The difference in speed will be unnoticable.

The correct way to do it is 1, because it is safest and best readable.

String.IsNullOrEmpty() is very much readable and does/works as intended, would be glad to stick to that.

Do you really have an edge case scenario where its falling short then please add the same to the question which would make it more relevant.

Use

String.IsNullOrEmpty()

that is the best way on .NET .

I personally always use String.IsNullOrEmpty() . I don't really think they do anything special other than check if it's either null or empty, so it shouldn't be any slower than a hand-written check.

Besides, sometimes you might be in a hurry and accidentally put the null check at the end, getting a nasty surprise. :)

Its better way and recomended to use String.IsNullOrEmpty for checking null or empty strings most rapidly. Because its inbuilt method in String.

In your samples, the solution 2 is the fastest. But it is a solution different from the others, since it doesn't check if the string is empty.

Otherwise, given that you want to check if a string is null or empty, the solution 5 is the fastest. String.IsNullOrEmpty() does exactly the same thing as solution 5 but adds a function call (if it isn't inlined at runtime). Still, I would recommand the first solution:

1/ the performance penalty is negligible

2/ It's easier to read

3/ it's a built-in method, so it's future-proof

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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