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

Which method is the fastest way to check for an empty string, and is there any particular case where any particular one is required.

1. String.IsNullOrEmpty() 2. str == null 3. str == null || str == String.Empty 4. str == null || str == "" 5. str == null || str.length == 0 
+4
source share
9 answers

Use parameter 1.

If you specifically want to check for null or empty strings, then there is no reason to use anything other than string.IsNullOrEmpty . This is the canonical way to do this in .NET, and any performance differences will almost certainly be minor.

This is an example tutorial on premature optimization ; by all means, write efficient code, but don’t waste time developing for it without any reasonable performance gain. Remember that your time as a developer is usually far more valuable than processor time.

Quote from Donald Knut:

We must forget about little efficiency, say, about 97% of the time: premature optimization is the root of all evil.

If this level of micro-optimization is really necessary for your application, then you should probably not use .NET.

+18
source

I found this site with some statistics on various methods: http://www.dotnetperls.com/empty-string

+3
source

Are you not indifferent too?

If the spaces are valid, use String.IsNullOrEmpty , otherwise use String.IsNullOrWhiteSpace (in .Net 4.0 or later). The latter is equivalent, but more productive 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

+2
source

The difference in speed will be imperceptible.

The correct way to do this is 1, because it is safer and easier to read.

+2
source

String.IsNullOrEmpty() very readable and does / works as intended, I would be happy to adhere to this.

Do you really have a scenario of a marginal case where its fall is shorter, then please add the same to the question, which will make it more relevant.

+1
source

Use

 String.IsNullOrEmpty() 

this is the best way for .NET.

+1
source

I personally always use String.IsNullOrEmpty() . I really don't think they are doing anything special except to check if it is either null or empty, so it should not be slower than a handwritten check.

In addition, sometimes you can rush and accidentally put a null check at the end, getting an unpleasant surprise. :)

0
source

Its the best way and it is recommended to use String.IsNullOrEmpty for fast and accurate search for a null or empty string. Because its a built-in method in String.

0
source

In your samples, solution 2 is the fastest. But this solution is different from others because it does not check if the string is empty.

Otherwise, given that you want to check if the string is empty or empty, solution 5 is the fastest. String.IsNullOrEmpty () does the same as solution 5, but adds a function call (if it is not inline at run time). However, I would recommend the first solution:

1 / performance penalty is negligible

2 / Easier to read

3 / it is a built-in method, therefore it is reliable

-1
source

Source: https://habr.com/ru/post/1389448/


All Articles