Pankaj Agarwal
Pankaj Agarwal

Reputation: 11309

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

Upvotes: 10

Views: 26095

Answers (9)

Will Vousden
Will Vousden

Reputation: 33388

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.

Upvotes: 23

ZombieSheep
ZombieSheep

Reputation: 29963

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

Upvotes: 5

Kevin Gosse
Kevin Gosse

Reputation: 39027

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

Upvotes: 0

Tudor
Tudor

Reputation: 62469

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

Upvotes: 0

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

Upvotes: 0

Richard Hanley
Richard Hanley

Reputation: 328

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

Upvotes: 3

aleroot
aleroot

Reputation: 72676

Use

String.IsNullOrEmpty()

that is the best way on .NET .

Upvotes: 1

V4Vendetta
V4Vendetta

Reputation: 38230

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.

Upvotes: 1

Euphoric
Euphoric

Reputation: 12849

The difference in speed will be unnoticable.

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

Upvotes: 2

Related Questions