Most advisable way of checking empty strings in C#

C#StringComparison

C# Problem Overview


What is the best way for checking empty strings (I'm not asking about initializing!) in C# when considering code performance?(see code below)

string a;

// some code here.......


if(a == string.Empty)

or

if(string.IsNullOrEmpty(a))

or

if(a == "")

any help would be appreciated. :)

C# Solutions


Solution 1 - C#

Do not compare strings to String.Empty or "" to check for empty strings.

Instead, compare by using String.Length == 0

The difference between string.Empty and "" is very small. String.Empty will not create any object while "" will create a new object in the memory for the checking. Hence string.empty is better in memory management. But the comparison with string.Length == 0 will be even faster and the better way to check for the empty string.

Solution 2 - C#

I think the best way is if(string.IsNullOrEmpty(a)) because it's faster and safer than the other methods.

Solution 3 - C#

string.IsNullOrEmpty(a)

it will check both NULL || EMPTY

this is the Implementation :

public static bool IsNullOrEmpty(string value)
{
    if (value != null)
    {
        return (value.Length == 0);
    }
    return true;
}

Solution 4 - C#

A late arrival:

if a == ""

will give rise to Code Analysis warning CA1820, so you definitely shouldn't do that. For a full analysis see CA1820: Test for empty strings using string length

Solution 5 - C#

You can use Length as well

string input = "";

if (input != null)
{
    if (input.Length == 0)
    {

    }
}  

   

Solution 6 - C#

Create an extension method for complete check:

public static bool IsEmpty(this string s)
{
  if(s == null) return true;
  return string.IsNullOrEmpty(s.Trim()); // originally only (s)
}

Sorry, not good code, fixed. Now this will tell you, if the string is empty or if is empty after trimming.

Solution 7 - C#

String.Empty value will be deciphered at run time only but on the other side "" value is known at the compile time itself.

That's the only difference between those two.

But coming to the best practice, if tomorrow M$ decides that the empty string value should be used as '' instead of "" due to some reason, then your code has to be changed every where. So in that case its best to use String.Empty.

Its the same practice used with Path.Combine as well.

Solution 8 - C#

This is covered and documented in the official code analysis rule CA1820: Test for empty strings using string length.

> Comparing strings using the String.Length property or the String.IsNullOrEmpty method is faster than using Equals. This is because Equals executes significantly more MSIL instructions than either IsNullOrEmpty or the number of instructions executed to retrieve the Length property value and compare it to zero.

So to check whether a string is empty, use a .Length 0 check (string.Length property):

"".Length == 0

If the string variable may be null, use string.IsNullOrEmpty().

If you want to interpret and accept whitespace as “empty”, use string.IsNullOrWhiteSpace().

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAllan ChuaView Question on Stackoverflow
Solution 1 - C#YogeshView Answer on Stackoverflow
Solution 2 - C#Gregory NozikView Answer on Stackoverflow
Solution 3 - C#Royi NamirView Answer on Stackoverflow
Solution 4 - C#DaveView Answer on Stackoverflow
Solution 5 - C#Sai Kalyan Kumar AkshinthalaView Answer on Stackoverflow
Solution 6 - C#Karel FrajtákView Answer on Stackoverflow
Solution 7 - C#ZenwalkerView Answer on Stackoverflow
Solution 8 - C#KissakiView Answer on Stackoverflow