10

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
6
  • 7
    The 1st one is the correct way. Because its inbuit in a string. Commented Jan 5, 2012 at 12:03
  • 3
    'str' is not a good variable name Commented Jan 5, 2012 at 12:08
  • In most of those examples there is no difference. For example testing to see if the length is 0 is checking to see if its an empty string. In one example just checking to see if its null is sort of silly, since the default value for a string is an empty string, not a null string. We are talking about performance differnces that you won't even begin to measure since they are so negligible. Stop trying to optimized trivial tasks. Commented Jan 5, 2012 at 12:20
  • 1
    This is unlikely to make any difference. Better worry about str == " ". Commented Jan 5, 2012 at 12:32
  • 1
    @Ramhound What makes you think the default value for string is an empty string? string is a reference type, its default value is null. Commented Jan 5, 2012 at 12:41

9 Answers 9

23

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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;

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

Comments

3

Found this website with some stats on the different methods: http://www.dotnetperls.com/empty-string

Comments

2

The difference in speed will be unnoticable.

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

Comments

1

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.

Comments

1

Use

String.IsNullOrEmpty()

that is the best way on .NET .

Comments

0

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

Comments

0

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. :)

Comments

0

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

4 Comments

I have to downvote this answer because the default value of a string variable is an empty string not a null string. Although everything you say about String.IsNullOrEmpty is correct.
I don't understand your point. Where do I talk about the default value of a string?
I made that point. Which is a negative to your statement that solution 2 is the fastest, since it really does not appear to do what the author actually wants to test for, since it has limited uses.
Well that's why I precised "But it is a solution different from the others, since it doesn't check if the string is empty. "

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.