Fun with Empty String.

I am faster.

Everyone uses pretty heavily  IsNullOrEmpty function that is a member of String class.  But sometimes we need to implement different behavior for an Empty value versus the null one.  There are multiple ways we can check for an Empty value.

Here are three different ways I come up with:

public bool IsEmpty_1(string text)
    return text == "";
public bool IsEmpty_2(string text)
    return text.Equals(string.Empty);
public bool IsEmpty_3(string text)
    return text.Length == 0;

After looking at these functions in Reflector, it was obvious that the method that uses Length should be the most efficient.  To prove it I created a little Console application that executes each comparison 100,000,000 times.  Here’s the code that does it:

equality code

Here’s the result:

equality output

The result above compares empty string “” to “text”.

"text" == ""
"text".Length == 0

You can see that statement with equality operator (==) executes slower than the one with Equals function.  However if we compare empty string with empty string we would have different result.

"" == ""
"".Length == 0

Here’s the output when comparing empty string with empty string.

equal output

I am more readable.

Efficient code is important but I would chose the most readable code.  Unfortunately we cannot prove with numbers which code is more readable.   It can be very subjective which code is easier to read but in any case I’ll be brave enough and make my recommendations.

I would extend String class with extension method.

public static class StringUtils
    public static bool IsEmpty(this string text)
        return text.Length == 0;

Now we can write code like this:


We can go further and create extension methods like IsNull and IsNullOrEmpty:

public static bool IsNullOrEmpty(this string text)
    return string.IsNullOrEmpty(text);

public static bool IsNull(this object obj)
    return obj == null;

You probably think why do we need to extend with  IsNullOrEmpty; it’s already part of String class.  You don’t but I think it’s more readable.  Look at two statements bellow. Which one makes more scenes to you?


One more note that IsNull extension method you can use with any object not just String.

List<int> numbers;
if (numbers.IsNull())

kick it on DotNetKicks.com


10 thoughts on “Fun with Empty String.

  1. blurring the lines between Static and Instance Methods…

    Saying foo.IsNullOrEmpty() and having it do other than puke when the foo instance is null is rather distracting from the idea that nullness implies no instance methods can be called.

    What a way to confuse the kids!

    (It is a neat trick though, but very black boxy.)

  2. Another cool comparison technique (that doesn’t work) is to use object.ReferenceEquals(str, string.Empty);
    Since strings are usually interned in the CLR, similar strings share the same reference, so we can compare them using object.ReferenceEquals().
    However (and this is why I said it doesn’t work) – not all string are interned, so it won’t work all the time. Oh well, I tried. :-)
    The performance however, is almost good as the Length comparison.

    BTW – here’s an example for empty string that is not interned:
    StringBuilder sb = new StringBuilder();
    string empty = sb.ToString();

  3. Not sure if you mentioned this but you always need to run the ALL of the test routines once before you start timing to make sure any first time startup allocations and assemblies loads have taken place, then without shutting down your test app you do the times and then you know have eliminated any first time startup issues

  4. @Omer,

    I believe that explains the shifting results for oper== & Equals.

    Oper== calls Object.ReferenceEquals first, then String.Equals, so, “” == “” hits on the ReferenceEquals, which is faster than String.Equals. “text” == “” fails on the ReferenceEquals and has to do the String.Equals anyway, so the initial ReferenceEquals is just a wasted call.

  5. @Dave,

    I can see your point. Someone else also told me that null.IsNullOrEmpty() doesn’t look right. However, for me it’s not a problem I find this syntax is easier to read.

    Like I said in my post that readability is subjective. You need to do what makes sense to you and your team.


    It’s not true in this case. You can run the tests in different order and you still will get the same numbers.

  6. I did IsNull() extension method in two projects and after looking the code base when the project was finished I decided to remove those and replace them with good old if (null == obj) code. The difference was huge. I could take a quick look and I can see where the null checks are made.

    I also thought that the code would be more readable with if (obj.IsNull()) type of code but that is not true. Checking for null values is specific case. Usually done e.g. when validating parameters etc. so it should be easy to spot those cases. If the check looks like any other method call then it will be harder. Also if you use IsNull() extension method you are introducing an exception to the rule. The rule is that if you try to use any property or method of a an object which is null you will get NullReferenceException:
    if (obj.IsValid);

    All those will throw NullReferenceException if obj is null. Do you really want to introduce IsNull() extension method which looks like normal method call but does not throw exception if obj is null?

  7. I’ve done this same exact spike test to determine the most efficient check for an empty string. Personally, I was somewhat surprised at the results. I have since incorporated it (comparing Length) into my personal development style. I’ve brought it light for a few colleagues of mine, and they were equally surprised. I have not heard anybody complain about readability. The only downside is that it is based upon the assumption the string is never null.

  8. @Mike D: That shouldn’t be a problem. If you need to do the same thing for If Null and If Empty, then you should use String.IsNullOrEmpty(). If you need to do different thing for each of those cases, then one must assume that you’ve already done the If Null and handled that case.

  9. this is the string. IsNullOrEmpty method…

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

    so no reason to do not use it since it check both null and empty string in the best way possible.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s