Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET string.IsNullOrWhiteSpace implementation

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?

like image 727
epignosisx Avatar asked Apr 20 '12 17:04

epignosisx


1 Answers

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.

like image 58
SLaks Avatar answered Oct 04 '22 11:10

SLaks