6

Folks, I was looking at the implementation of string.IsNullOrWhiteSpace in:

http://typedescriptor.net/browse/types/9331-System.String

Here is the implementation:

public static bool IsNullOrWhiteSpace(string value)
{
    if (value == null)
    {
        return true;
    }
    for (int i = 0; i < value.Length; i++)
    {
        if (char.IsWhiteSpace(value[i]))
        {
        }
        else
        {
            goto Block_2;
        }
    }
    goto Block_3;
    Block_2:
    return false;
    Block_3:
    return true;
}

Question: Isn't this over complicated? Can't the following implementation do the same job and be easier on the eye:

bool IsNullOrWhiteSpace(string value)
{
    if(value == null)
    {
        return true;
    }   
    for(int i = 0; i < value.Length;i++)
    {
        if(!char.IsWhiteSpace(value[i]))
        {
            return false;
        }
    }
    return true;
}

Is this implementation incorrect? Does it have a performance penalty?

1
  • 2
    That's essentially what it is doing - you are not looking at the real source code, just what was gathered from reflection Commented Apr 20, 2012 at 17:57

4 Answers 4

19

The original code (from the reference source) is

public static bool IsNullOrWhiteSpace(String value) {
    if (value == null) return true; 

    for(int i = 0; i < value.Length; i++) { 
        if(!Char.IsWhiteSpace(value[i])) return false; 
    }

    return true;
}

You're seeing the output of a poor decompiler.

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

6 Comments

Note that this could be considerably shortened with the following code: return value == null || value.All(Char.IsWhiteSpace);
@SLaks - Could you add a link to or documentation on "the reference source"?
Could be shorter now too with return value?.All(Char.IsWhiteSpace) ?? true;
|
8

You are looking at C# that was recreated from the disassembled IL. I am sure that the actual implementation is closer to your example and does not use labels.

Comments

3

Shown below is an extension method I needed for older versions. I am not sure where I obtained the code from:

public static class StringExtensions
    {
        // This is only need for versions before 4.0
        public static bool IsNullOrWhiteSpace(this string value)
        {
            if (value == null) return true;
            return string.IsNullOrEmpty(value.Trim());
        }
    }

Comments

3

It must be typedescriptor's disassembler doing that.

When I look at the same function with JetBrain's dotPeek it looks like this:

 public static bool IsNullOrWhiteSpace(string value)
    {
      if (value == null)
        return true;
      for (int index = 0; index < value.Length; ++index)
      {
        if (!char.IsWhiteSpace(value[index]))
          return false;
      }
      return true;
    }

Comments

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.